Error as Function definitions are not permitted in this context.

1 vue (au cours des 30 derniers jours)
vetri veeran
vetri veeran le 4 Avr 2017
Réponse apportée : KSSV le 4 Avr 2017
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.

Réponse acceptée

KSSV
KSSV le 4 Avr 2017
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.

Plus de réponses (0)

Catégories

En savoir plus sur Adding custom doc dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by