Use eulers Method using matlab with 2 different step sizes. Please show code for both problems.

8 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 14 日
編集済み: madhan ravi 2020 年 6 月 14 日
What did you try? Paste what you have tried.
I've just been looking up scripts and plugging in my dy/dx but nothing works.
Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日
How are you calling the function 'euler'? Also, It is better to attach code as text instead of an image. We cannot edit the code using an image.
function euler(func,y0,delt,tf)
t=0:delt:tf;
y(1)=y0;
for i = 1:length(t)-1
y(i+1)=y(i)+delt*(feval(func,t(i)));
end
plot (t,y);
xlabel('time')
ylabel('y')
disp(y(end))
Ameer Hamza
Ameer Hamza 2020 年 6 月 15 日
Original Question asked by Kendrick Lolange:
Title: Use eulers Method using matlab with 2 different step sizes. Please show code for both problems.
Rena Berman
Rena Berman 2020 年 10 月 12 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日
Since this is a homework question, I will not give a complete code. However, I will point you to resources that can help you to understand how to implement the Euler method in MATLAB.
You can download the zip file given in this answer: https://www.mathworks.com/matlabcentral/answers/98293-is-there-a-fixed-step-ordinary-differential-equation-ode-solver-in-matlab-8-0-r2012b#answer_107643 and study the code of ode1.m. It is the implementation of the Euler method provided in very early releases of MATLAB. It is no longer included in MATLAB, but it is still useful to understand the implementation of the Euler method.
Following FEX packages are also helpful to learn about Euler method and its implementation MATLAB:

4 件のコメント

This isn't a homework problem its just review for a quiz please show code.
Following code shows how to solve the two equations in you question. Study this code
figure;
euler(@fcn1, 1, 0.1, 2) % h=0.1
title('y''(x)=2*x*y h=0.1');
figure;
euler(@fcn1, 1, 0.05, 2) % h=0.05
title('y''(x)=2*x*y h=0.05');
figure;
euler(@fcn2, 1, 0.1, 2) % h=0.1
title('y''(x)=(x-y)/(x+2y) h=0.05');
figure;
euler(@fcn2, 1, 0.05, 2) % h=0.05
title('y''(x)=(x-y)/(x+2y) h=0.05');
function euler(func,y0,delt,tf)
t=0:delt:tf;
y(1)=y0;
for i = 1:length(t)-1
y(i+1)=y(i)+delt*func(t(i), y(i));
end
plot (t,y);
xlabel('time')
ylabel('y')
disp(y(end))
end
function dy = fcn1(x, y)
dy = 2*x*y;
end
function dy = fcn2(x, y)
dy = (x-y)./(x+2*y);
end
Thank you! These are the errors correct?
29.4986
39.0930
0.9769
0.9886
Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日
No, these are the solution at x=2 given by Euler Method. To find the error, you will need to solve the equation analytically.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2020 年 6 月 14 日

コメント済み:

2020 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by