# loglog plot in MATLAB

• Mar 30th 2011, 07:51 PM
Mollier
loglog plot in MATLAB
Hi,

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

http://i163.photobucket.com/albums/t...ion_embree.jpg

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 \$\displaystyle y'=y\$. What I end up with is quite far from what i wanted, namely:

http://i163.photobucket.com/albums/t...on_mollier.jpg

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 \$\displaystyle 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.
• Mar 30th 2011, 08:02 PM
CaptainBlack
Quote:

Originally Posted by Mollier
Hi,

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

http://i163.photobucket.com/albums/t...ion_embree.jpg

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 \$\displaystyle y'=y\$. What I end up with is quite far from what i wanted, namely:

http://i163.photobucket.com/albums/t...on_mollier.jpg

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 \$\displaystyle 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
• Mar 31st 2011, 07:43 PM
Mollier
Quote:

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:

http://i163.photobucket.com/albums/t...t_selector.jpg

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?