Results 1 to 3 of 3

Math Help - loglog plot in MATLAB

  1. #1
    Member Mollier's Avatar
    Joined
    Nov 2009
    From
    Norway
    Posts
    234
    Awards
    1

    loglog plot in MATLAB

    Hi,

    I was reading some notes on-line on linear multi-step methods and saw this graph:



    I thought it would be good MATLAB-practice to replicate the graph and gave it a go.

    To start off with, I did not include Adams-Bashfort, and just used Euler's to solve the equation y'=y. What I end up with is quite far from what i wanted, namely:



    Not very linear... The code that includes the loglog plot is;

    Code:
    yprime = @(t,y) y; 
    
    tspan = [0 1]; 
    y0 = 1; 
    h = logspace(-3,0);
    n = length(h);
    absErrorEuler = zeros(n,1);
    
    for i = 1:n
        [teuler,yeuler] = euler(yprime,tspan,y0,h(i));
        absErrorEuler(i) = abs(yeuler(end)-exp(1));
    end
    
    figure(2), clf,hold on
    set(gca,'XDir','reverse')
    loglog(h,absErrorEuler,'kd')
    xlabel('h'), ylabel('error at t=1')
    The implementation of Euler's is;

    Code:
    function [t,y] = euler(yprime, tspan, y0, h)
    
    t0 = tspan(1); tfinal = tspan(end);
    
    % set up the t values at which we will approximate the solution
    t=[t0:h:tfinal];
    
    % include tfinal even if h does not evenly divide tfinal-t0
    if t(end)~=tfinal, t = [t tfinal]; end
    
    % execute Euler's method
    y = [y0 zeros(length(y0),length(t)-1)];
    
        for j=1:length(t)-1
            y(:,j+1) = y(:,j) + h*feval(yprime,t(j),y(:,j));
        end
    Before dealing with the details of how to put 10^0,10^1... on the x and y axes, I need to deal with the fact that the plot should show a linear relationship.

    Any suggestions are welcome.

    Thanks.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Mollier View Post
    Hi,

    I was reading some notes on-line on linear multi-step methods and saw this graph:



    I thought it would be good MATLAB-practice to replicate the graph and gave it a go.

    To start off with, I did not include Adams-Bashfort, and just used Euler's to solve the equation y'=y. What I end up with is quite far from what i wanted, namely:



    Not very linear... The code that includes the loglog plot is;

    Code:
    yprime = @(t,y) y; 
    
    tspan = [0 1]; 
    y0 = 1; 
    h = logspace(-3,0);
    n = length(h);
    absErrorEuler = zeros(n,1);
    
    for i = 1:n
        [teuler,yeuler] = euler(yprime,tspan,y0,h(i));
        absErrorEuler(i) = abs(yeuler(end)-exp(1));
    end
    
    figure(2), clf,hold on
    set(gca,'XDir','reverse')
    loglog(h,absErrorEuler,'kd')
    xlabel('h'), ylabel('error at t=1')
    The implementation of Euler's is;

    Code:
    function [t,y] = euler(yprime, tspan, y0, h)
    
    t0 = tspan(1); tfinal = tspan(end);
    
    % set up the t values at which we will approximate the solution
    t=[t0:h:tfinal];
    
    % include tfinal even if h does not evenly divide tfinal-t0
    if t(end)~=tfinal, t = [t tfinal]; end
    
    % execute Euler's method
    y = [y0 zeros(length(y0),length(t)-1)];
    
        for j=1:length(t)-1
            y(:,j+1) = y(:,j) + h*feval(yprime,t(j),y(:,j));
        end
    Before dealing with the details of how to put 10^0,10^1... on the x and y axes, I need to deal with the fact that the plot should show a linear relationship.

    Any suggestions are welcome.

    Thanks.
    I would suggest you check that you are running the right version of the script, then try using the GUI version (use the plot selector) of loglog.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member Mollier's Avatar
    Joined
    Nov 2009
    From
    Norway
    Posts
    234
    Awards
    1
    Quote Originally Posted by CaptainBlack View Post
    I would suggest you check that you are running the right version of the script, then try using the GUI version (use the plot selector) of loglog.

    CB
    When I use the plot selector I get:



    Running the plot selector gives loglog(h,absErrorEuler,'DisplayName','h,absErrorEu ler');figure(gcf)
    in the command window, which I guess is not enough the give the scale I want. That is, when I use that command in my script, I get the "wrong" scale.

    Any suggestions?
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. 3D plot of z = (Matlab)
    Posted in the Math Software Forum
    Replies: 4
    Last Post: July 14th 2011, 08:22 PM
  2. Kac Formula plot with MatLab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 14th 2010, 07:33 AM
  3. help me to plot this equation in Matlab....
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 23rd 2009, 09:38 PM
  4. [Matlab] 2D Sequence Plot in 3D
    Posted in the Math Software Forum
    Replies: 2
    Last Post: April 23rd 2009, 04:28 AM
  5. MatLab Plot
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 2nd 2008, 10:59 AM

Search Tags


/mathhelpforum @mathhelpforum