Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Question regarding ode45 utilization

2 vues (au cours des 30 derniers jours)
Luke
Luke le 14 Nov 2014
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hey, I'm working on a homework assignment that requires the use of the ode45 function, and I'm having some trouble wrapping my head around it. Here's my code so far:
t = 0:0.1:50; %timescale
[t,y] = ode45('MyStates', t, [2 -0.5 1.047 -2.094]);
and here's the MyStates function:
function dy = MyStates(t,y)
dy = zeros(4,1);
dy(1) = x(2);
dy(2) = (-14/50)*x(1) + ((14*10)/50)*theta(1);
dy(3) = theta(2);
dy(4) = ((3*14)/(50*10))*x(1) - ((6*14)/50)*theta(1);
end
The function keeps returning the message "Error: Function definitions are not permitted in this context." I really don't know what that means and what I'm doing wrong, let alone how to fix it, so I've been spinning my wheels for the past 30 minutes.
Thanks in advance!

Réponses (2)

Star Strider
Star Strider le 14 Nov 2014
You have to save your ‘MyStates’ function to its own separate function file, ‘MyStates.m’.
You need to add ‘theta’ as an argument because it will not pick it up from the workspace.
function dy = MyStates(t,y,theta)
dy = zeros(4,1);
dy(1) = x(2);
dy(2) = (-14/50)*x(1) + ((14*10)/50)*theta(1);
dy(3) = theta(2);
dy(4) = ((3*14)/(50*10))*x(1) - ((6*14)/50)*theta(1);
end
Your ode45 call needs to change as well:
[t,y] = ode45(@(t,y) MyStates(t,y,theta), t, [2 -0.5 1.047 -2.094]);
The ODE function argument to ode45 is now an anonymous function that will pick up ‘theta’ from your workspace and pass it to ‘MyStates’, as well as make ode45 happy with the version of ‘MyStates’ it sees.
I didn’t run your code but this should work.

Torsten
Torsten le 14 Nov 2014
Replace x(1),x(2),theta(1) and theta(2) in MyStates by y(1),y(2),y(3) and y(4).
Best wishes
Torsten.

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by