Info

此问题已关闭。 请重新打开它进行编辑或回答。

Question regarding ode45 utilization

2 次查看(过去 30 天)
Luke
Luke 2014-11-14
关闭: MATLAB Answer Bot 2021-8-20
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!

回答(2 个)

Star Strider
Star Strider 2014-11-14
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 2014-11-14
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.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by