Differential equation Eulers method plotting vs. Exact solution

I am trying to find the solutions to the differential equation 2*x*y*(1-y) using Euler's method and then comparing with the exact solution. I'm want to plot different sub-intervals (n value) so I can see the comparison. I'm having a hard time figuring out the Euler's solutions though. I feel like I'm very close but I've confused the hell out of myself with so many different variables and trying to think logically.
My eulers function looks like this right now:
function sequence = eulers(f,a,b,y0,n)
h=(b-a)/n; % interval length
x(1)=a;
y(1)=y0;
for K=1:n
x(n+1)=x(n)+h;
y(n+1)=y(n)+h*f(x(n),y(n));
end
sequence = [x(n),y(n)]
end
And I'm calling the eulers/exact solutions as well as plotting it like this:
clear;
S = [5, 10, 30, 200]; % n-values
numx = length(S);
Y = zeros(1,numx);
for T = 1:numx
Y(T) = eulers(@(x,y) 2*x*y*(1-y),0,2,2, S(T));
end
% calculate the exact solution
fdash = @(x,y) 2*x*y*(1-y);
tspan = [0,2];
yzero = 2;
[xexact,yexact] = ode45(fdash,tspan,yzero);
plot(x,y,'g',xexact,yexact, 'k')
title(['Eulers Method vs Exact Solution'])
legend('Approximate','Exact');
Another set of eyes would be greatly appreciated

10 个评论

You cannot call the solution determined by ODE45 "exact". It is an approximation also.
1. Your loop index in eulers is K, not n.
2. eulers returns a vector of two elements (namely b and the solution of your ODE at x=b). So you can't store them in Y(T).
3. For plotting, x and y are undefined.
Best wishes
Torsten.
Torsten,
I fixed up the loop index. Thanks for pointing that out
I made some changes to the calling of it based on your suggestions and cleaned up some variables to make it more clear:
clear;
S = [5, 10, 30, 200]; % n-values
numS = length(S);
eulerapprox = zeros(1,2);
for T = 1:numS
eulerapprox = eulers(@(x,y) 2*x*y*(1-y),0,2,2, S(T));
end
% calculate the exact solution
fdash = @(x,y) 2*x*y*(1-y);
tspan = [0,2];
yzero = 2;
[xexact,yexact] = ode45(fdash,tspan,yzero);
plot(eulerapprox,'g',xexact,yexact, 'k')
title(['Eulers Method vs. Exact Solution'])
legend('Approximate','Exact');
I'm getting the "Data must be a single matrix Y or a list of pairs X,Y." I suspect it is because of the eulerapprox in the plot function.
Please post the complete error message. It is not efficient if we guess which line causes the error.
Error using plot Data must be a single matrix Y or a list of pairs X,Y.
Yes, you are right. You will have to call plot with at least two (x,y) data points. Now you call it with "eulerapprox" which is an (1,2) vector.
What's the sense of the array "S" when you only use the last result from the loop
for T = 1:numS
eulerapprox = eulers(@(x,y) 2*x*y*(1-y),0,2,2, S(T));
end
?
Best wishes
Torsten.
I just thought the 1,2 vector would be be able to now store the x,y values.
And so with the loop is it an issue with the vector size. should it be
eulerapprox = zeros(200,1;
so that way it's a big vector, which can store enough values
To plot your solution, you will need (x(K),y(K)) for K=1:n. So you could try
a=0;
b=2;
y0=2;
n=50;
f=@(x,y) 2*x*y*(1-y);
h=(b-a)/n; % interval length
x(1)=a;
y(1)=y0;
for K=1:n
x(K+1)=x(K)+h;
y(K+1)=y(K)+h*f(x(K),y(K));
end
plot (x,y);
Best wishes
Torsten.
That code would only plot for one n value at a time. I'm trying to plot multiple n values on the same curve.
Then use a nested for-loop.
The outer loop changes n, the inner loop computes the solution for that particular n.
Best wishes
Torsten.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2015-5-26

评论:

2015-5-27

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by