# Thread:

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.

2. Originally Posted by Mollier
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

3. Originally Posted by CaptainBlack
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?
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 4, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.947350800037384, "perplexity_flag": "middle"}