How can I fix this?
古いコメントを表示
%Euler method
%Forward, Backward, Modified, Predictor-Corrector, Analytical Solution, and Absolute Error in a table using %fprintf function.
%dy/dx=2xy from x=1 to x=1.5. y(1)=1 stepsize is 0.05;
dx=0.05;
nfinal=0.5;
nsteps=(nfinal/dx);
xi=zeros(1,nsteps);
yF=zeros(1,nsteps);
yB=zeros(1,nsteps);
yM=zeros(1,nsteps);
yP=zeros(1,nsteps);
yC=zeros(1,nsteps);
%yy=zeros(1,nsteps);
%Initial Conditions
xi=1;
yF=1;
yB=1;
yM=1;
yP=1;
yC=1;
yy=1
fprintf('\n Time Forward_Mehtod Backward_Method Modified_Method\')
fprintf('\n -----------------------------------------------------------------------------\n')
for j=1:nsteps
xi=j*dx;
%Forward Method
yF=yF+(2*(xi)*yF)*dx;
xinew(j)=xi;
yFnew(j)=yF;
%Backward Method
yB=-yB/(dx*(2*xi)-1);
xinew(j)=xi;
yBnew(j)=yB;
%Modified
yM=yM+(dx/2)*((2*xi*yM)+2*(xi+dx)*(yM+2*xi*yM*dx));
xinew(j)=xi;
yMnew(j)=yM;
%Predictor Corrector Method
yP=yP+dx*(2*xi*yP); %Predictor
yC=yC+dx*(2*xi*yP);
xinew(j)=xi;
yPnew(j)=yP;
yCnew(j)=yC;
%Analytic Solution
yy=exp((xi^2)-1);
end
fprintf('%6.2g %20.2f %20.2f %20.2f %20.2f %20.2f\n', [xi, yFnew, yBnew, yMnew, yPnew, yCnew, yy])
plot(xinew,yFnew,'b--','linewidth',3)
hold on
plot(xinew,yBnew,'g--','linewidth',3)
plot(xinew,yMnew,'r--','linewidth',3)
plot(xinew,yPnew,'*','linewidth',3)
legend('Forward Method','Backward Method','Modified Method','Predictor-Corrector')
xlabel('time')
ylabel('y')
4 件のコメント
Walter Roberson
2018 年 3 月 9 日
What leads you to suspect that there is something that needs to be fixed?
Romina Doubnia
2018 年 3 月 9 日
Torsten
2018 年 3 月 9 日
If it helps: The analytical solution for your problem is
y(x) = exp(x^2-1)
Best wishes
Torsten.
Romina Doubnia
2018 年 3 月 10 日
採用された回答
その他の回答 (1 件)
John D'Errico
2018 年 3 月 9 日
編集済み: John D'Errico
2018 年 3 月 9 日
I'm not going to debug that confusing mess of code. Sorry. And it looks as if my statement about the solution being an oscillatory one might make sense, if your predictions were correct. The table that you produce is a complex mess, making it appear as if the solution is oscillatory. In fact, that table is utterly confusing, showing 5 columns, with only 3 labels on top.
In fact, the solution is easy to compute analytically. (Hint: separation of variables.) It is:
y = exp(x^2-1)
So plotting the analytical solution, we see:

This is what you should hope to see. And, since the problem is not stiff at all, Euler's method should work reasonably well, subject to the caveat that the step size may be insufficiently small.
So, I'll give you a simple code for Euler's method here. (You did make a credible stab at the problem, although I won't write the rest of it for you.)
dx = 0.05;
x = 1:.05:1.5;
y = zeros(size(x));
y(1) = 1;
for n = 2:numel(x);
y(n) = y(n-1) + 2*x(n-1)*y(n-1)*dx;
end
ezplot(@(x) exp(x.^2 - 1),[1,1.5])
hold on
plot(x,y,'ro')

That is what you SHOULD get. You can add loops to do the other methods, based on what I did.
I would in fact recommend that you keep the methods separate, since as you wrote it, you are probably screwing things up by trying to do all three methods in one loop.
3 件のコメント
Romina Doubnia
2018 年 3 月 10 日
Romina Doubnia
2018 年 3 月 10 日
John D'Errico
2018 年 3 月 10 日
Yes. Each method would give you different predictions, because they have subtly different behavior and accuracy. As you can see, the simple Euler method loop I wrote is not that accurate. But the other methods might be more accurate (or potentially less so.) All will probably have the correct general behavior.
Note that if you cut the step size, you would expect the predictions to follow the correct curve more accurately.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!