loglog plot in MATLAB
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 . What I end up with is quite far from what i wanted, namely:
Not very linear... The code that includes the loglog plot is;
The implementation of Euler's is;
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));
figure(2), clf,hold on
xlabel('h'), ylabel('error at t=1')
Before dealing with the details of how to put on the x and y axes, I need to deal with the fact that the plot should show a linear relationship.
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
% 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)];
y(:,j+1) = y(:,j) + h*feval(yprime,t(j),y(:,j));
Any suggestions are welcome.
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.
Originally Posted by Mollier
When I use the plot selector I get:
Originally Posted by CaptainBlack
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.