Error as Function definitions are not permitted in this context.

2 ビュー (過去 30 日間)
vetri veeran
vetri veeran 2017 年 4 月 4 日
回答済み: KSSV 2017 年 4 月 4 日
Hi all,
I am getting error as Function definitions are not permitted in this context.
My code:
function dydt = odefcn(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (A/B)*t*y(1);
A= 1;
B= 2;
tspan=[0 5];
y0 = [0 0.01];
[t,y]= ode15s(@(t,y) odefcn(t,y,A,B), tspan, y0);
plot(t,y(:,1),'-o',t,y(:,2),'-.' )
Could you please help me.

採用された回答

KSSV
KSSV 2017 年 4 月 4 日
You have two options:
function Main
A= 1;
B= 2;
tspan=[0 5];
y0 = [0 0.01];
[t,y]= ode15s(@(t,y) odefcn(t,y,A,B), tspan, y0);
plot(t,y(:,1),'-o',t,y(:,2),'-.' )
end
function dydt = odefcn(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (A/B)*t*y(1);
end
Save the above lines in the m file and name it Main.m and run.
Or save the odefcn function alone in as function in file odefcn.m and call it file.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by