Euler method: ODE with different initial conditions

5 ビュー (過去 30 日間)
Atom
Atom 2023 年 1 月 21 日
編集済み: Torsten 2023 年 1 月 21 日
I want to solve an ODE by Euler method with different initial conditions.
I have used for y(1)=0.5:0.05:1.5; as the different initial conditions. But it given an error
for y(1)=0.5:0.05:1.5;
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
How to modify the code so that I can able to run a loop for different values of y(1).
NB: Tried with
for v(1) = [0.05 0.01 0.15 0.2]
end
but got the error.
h=0.5;
x=0:h:4;
y=zeros(size(x));
for y(1)=0.5:0.05:1.5;
n=numel(y);
for i = 1:n-1
dydx= -2*x(i).^3 +12*x(i).^2 -20*x(i)+8.5 ;
y(i+1) = y(i)+dydx*h ;
fprintf('="Y"\n\t %0.01f',y(i));
end
figure;
plot(x,y);
end
  2 件のコメント
Torsten
Torsten 2023 年 1 月 21 日
Make the code a function and call the function in a loop for different initial values y(1).
Atom
Atom 2023 年 1 月 21 日
How to do that? Please suggest...

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

採用された回答

Torsten
Torsten 2023 年 1 月 21 日
編集済み: Torsten 2023 年 1 月 21 日
Y0 = 0.5:0.05:1.5;
hold on
for i = 1:numel(Y0)
y0 = Y0(i);
[x,y] = euler(y0);
plot(x,y)
end
hold off
function [x,y] = euler(y0)
h=0.5;
x=0:h:4;
n = numel(x);
y=zeros(1,n);
y(1) = y0;
for i = 1:n-1
dydx= -2*x(i).^3 +12*x(i).^2 -20*x(i)+8.5 ;
y(i+1) = y(i)+dydx*h ;
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by