RK4 help (someone help fix this)

so i made the code but i only get the first two values then the values start to equal zeros wich shouldnt hapend.
this is the code :
x_start=0; %valeeur initial de x
x_end=1; %valeur final de x
n_nodes=20;
h=(x_end-x_start)/(n_nodes-1);
x=linspace(x_start, x_end, n_nodes);
y_RK4=zeros(1,n_nodes);
y_RK4(1)=3;
f=@(x,y) (-0.3*y+2*exp(-x));
for i=1,n_nodes-1;
k1=h*f(x(i),y_RK4(i));
k2=h*f((x(i)+(h/2)),(y_RK4(i)+(k1/2)));
k3=h*f((x(i)+(h/2)),(y_RK4(i)+(k2/2)));
k4=h*f((x(i)+h),(y_RK4(i)+k3));
y_RK4(i+1)=y_RK4(i)+(k1+(2*k2)+(2*k3)+k4)/6;
end

 採用された回答

Torsten
Torsten 2024 年 11 月 25 日

2 投票

Use
for i=1:n_nodes-1
instead of
for i=1,n_nodes-1;

3 件のコメント

Aya
Aya 2024 年 11 月 25 日
MAN THANK YOU SM i was about to cry, its such a stupid mistake cus i didnt notice it since we had four difrent functions to plot. THANK YOU SO MUUUUCH AGAIN THANKS TO YOU I WONT CRY THIS NIGHT.
Image Analyst
Image Analyst 2024 年 11 月 27 日
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
For full details on how to earn reputation points see: https://www.mathworks.com/matlabcentral/answers/help?s_tid=al_priv#reputation
Aya
Aya 2024 年 12 月 4 日
aha i did thanks man im new to matlab so i didnt know much :)))))

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

その他の回答 (0 件)

カテゴリ

質問済み:

Aya
2024 年 11 月 25 日

コメント済み:

Aya
2024 年 12 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by