フィルターのクリア

what should I do. HELP function

2 ビュー (過去 30 日間)
Bilal Ates
Bilal Ates 2020 年 6 月 26 日
コメント済み: Bilal Ates 2020 年 6 月 28 日
[y, z]=rungeKutta(2,4,0,1,10000)
function dydx=f(x,y,z)
dydx=-2*y + 4*exp(-x) + exp(-1000*(z^2));
end
function dzdx=g(y,z)
dzdx=-y*(z^2)/3;
end
function [y,z]=rungeKutta(y0,z0,x0,xn,n)
h=(xn-x0)/n;
x=x0;
y=y0;
x=x0;
for i=1:n
k0=h*f(x,y,z);
l0=h*g(y,z);
k1=h*f(x+h/2, y+ k0/2, z+ lo/2);
l1=h*g(y+ k0/2, z+ lo/2);
k2=h*f(x+h/2, y+ k1/2, z+ l1/2);
l1=h*g(y+ k1/2, z+ l1/2);
k3=h*f(x+h, y+ k2, z+ l2);
l3=h*g(y+ k2, z+ l2);
y=y + (k0+2*k1+2*k2+k3)/6;
z=z + (l0+2*l1+2*l2+l3)/6;
end
end
  1 件のコメント
Bilal Ates
Bilal Ates 2020 年 6 月 26 日
You are a great detail....

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

採用された回答

Gaurav Aggarwal
Gaurav Aggarwal 2020 年 6 月 26 日
編集済み: Gaurav Aggarwal 2020 年 6 月 26 日
Hi Bilal,
Your code seems to have many undefined variables, like z in line 15, lo in line17, l2 in line 22.
See if the following code works for you and gives the output according to your expectations.
[y, z]=rungeKutta(2,4,0,1,10000)
function dydx=f(x,y,z)
dydx=-2*y + 4*exp(-x) + exp(-1000*(z^2));
end
function dzdx=g(y,z)
dzdx=-y*(z^2)/3;
end
function [y,z]=rungeKutta(y0,z0,x0,xn,n)
h=(xn-x0)/n;
x=x0;
y=y0;
x=x0;
z=z0; %% z added
for i=1:n
k0=h*f(x,y,z);
l0=h*g(y,z);
k1=h*f(x+h/2, y+ k0/2, z+ l0/2); %% lo -> l0
l1=h*g(y+ k0/2, z+ l0/2);
k2=h*f(x+h/2, y+ k1/2, z+ l1/2);
l2=h*g(y+ k1/2, z+ l1/2); %% variable name l1 -> l2
k3=h*f(x+h, y+ k2, z+ l2);
l3=h*g(y+ k2, z+ l2);
y=y + (k0+2*k1+2*k2+k3)/6;
z=z + (l0+2*l1+2*l2+l3)/6;
end
end
Hope it helps. Thanks.
  3 件のコメント
Gaurav Aggarwal
Gaurav Aggarwal 2020 年 6 月 28 日
Did it work for you? If so, it would be great if you accept the answer and close this query. Thanks.
Bilal Ates
Bilal Ates 2020 年 6 月 28 日
worked. I thank you

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 6 月 26 日
As stated on this documentation page "Script files cannot have the same name as a function in the file." Either rename your script file to something other than rungeKutta.m or rename the function you define on line 9 of rungeKutta.m to something other than rungeKutta.
  2 件のコメント
Gaurav Aggarwal
Gaurav Aggarwal 2020 年 6 月 26 日
編集済み: Gaurav Aggarwal 2020 年 6 月 26 日
After defining some variables, the script worked for me. The script's name and the function name were same in my case. As per my understanding, it doesn't matter as long as the function call is within the file scope, which seems to be the case with Bilal's (acc. to the code he provided).
Bilal Ates
Bilal Ates 2020 年 6 月 28 日
Thank you all for your help

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

カテゴリ

Help Center および File ExchangeTiming and presenting 2D and 3D stimuli についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by