ODE45 what to put into the function argument

Hello, In a file a have an attachment to a problem of modeling a pendulum. It has a format in the code that I have to follow exactly as is, and I was wondering if I could get some help with what to put into the ODE 45 part. Here is the code
function ydot = InvPend1(y,g,L,k1,k2)
% initialize ydot array
ydot = zeros(2,1);
u = k1*y(1)+k2*y(2);
ydot(1) = y(2);
ydot(2) = (g/L)*sin(y(1))-(1/L)*cos(y(1))*u;
g = 9.8; L=1;k1=12;k2=2;
% Set initial conditions
y0 =[10*pi/180;0];
% Set Simulation time span
tspan = [0 8];
% Define function a handle
thetadotdot = @InvPend1;
[T,Y] = ode45(@(y,g,L,u) InvPend1,tspan,y0)
But I have a lot of difficulty figuring out what to put in the InvPed1 argument, and in general I am struggling on how to use the ode45 function because I am having a difficult time figuring out what to put as its first argument.
I could also use some help with the subplots. How do I plot theta vs. time? plot(T,Y)?? But what is U array? I think my problem here is that I don't fully comprehend what the output [T, Y] will be. Will Y be the original matrix [Y(1);Y(2)], meaning this ode45 will output the theta and derivative of theta for all times in the time span?
% Use subplot to create top plot
subplot(2,1,1);
% plot theta (degrees) vs. time
plot(T,Y);
% Set xlabel
xlabel('time-seconds');
% Set yLabel
ylabel('theta-deg');
% Compute U array
U = k1*Y(1) + k2*Y(2);
% plot u vs. time
plot(T,U);
% Set xlabel
xlabel('time-seconds');
% Set ylabel
ylabel('u-rad/sec^2');

 採用された回答

Star Strider
Star Strider 2014 年 8 月 14 日

0 投票

The ODE function arguments have a specific format, and have to include your independent variable (usually t) as the first argument (even if you don’t use it):
function ydot = InvPend1(t,y,g,L,k1,k2)
Your argument to ode45 becomes:
[T,Y] = ode45(@(t,y) InvPend1(t,y,g,L,k1,k2),tspan,y0)
I don’t know what you’re doing with this line, so I suggest you delete it:
thetadotdot = @InvPend1;
I got your code to work, with ‘InvPend1’ as an anonymous function. Your function file is otherwise fine as it is (once you change the first line to include t), so my code will work as an illustration. (Anonymous functions are easier for me to work with.)
You calculate ‘u’ within your ‘InvPend1’, so you don’t need to pass it as an argument. If you want to use it in your plot, you simply need to calculate it:
u = k1*Y(:,1)+k2*Y(:,2);
since your calculated values for Y is a (81x2) matrix (when I ran it), you need to refer to your values by column references.
I don’t know what you want to do w.r.t. your ‘subplots’ because I don’t know your requirements. I plotted everything in one plot here.
My code (for your reference):
InvPend1 = @(t,y,g,L,k1,k2) [y(2); (g/L)*sin(y(1))-(1/L)*cos(y(1))*(k1*y(1)+k2*y(2))];
g = 9.8; L=1;k1=12;k2=2;
% Set initial conditions
y0 =[10*pi/180;0];
% Set Simulation time span
tspan = [0 8];
[T,Y] = ode45(@(t,y) InvPend1(t,y,g,L,k1,k2),tspan,y0);
u = k1*Y(:,1)+k2*Y(:,2);
figure(1)
plot(T, Y, T, u)
grid
legend('Y(t)', 'Y''(t)', 'u(t)', 'Location','NE')

6 件のコメント

Rick
Rick 2014 年 8 月 14 日
編集済み: Rick 2014 年 8 月 14 日
Would this work?? The thing is, you are being logical, but we aren't allowed to change the argument list on the exam, so what would you do if you weren't allowed to change any of the code already in the code in the problem statement?
ydot = InvPend1(y,g,L,k1,k2);
% for part b)
y0 = [10*pi/180;0];
tspan = [0 8];
f = @(y,g,L,k1,k2) ydot;
[T,Y] = ode45(f,tspan,y0);
Star Strider
Star Strider 2014 年 8 月 14 日
編集済み: Star Strider 2014 年 8 月 14 日
No, it won’t. I just did that experiment, and the ODE solver insists on passing two variables, t and y, to the ODE function. If you use it as you listed, with one y argument only, you will get a ‘too many input arguments’ error when the ODE solver attempts to pass both t and y to your ODE function, and the code will stop. To use the ODE functions, it appears to be necessary to pass both t and y to your ODE functions:
ydot = InvPend1(t,y,g,L,k1,k2);
f = @(t,y) InvPend1(t,y,g,L,k1,k2);
If a condition of your exam is that you have to omit t in the argument list, it appears that you will not be able to run it. I even used the null argument ‘~’ in place of t and it failed.
I have no idea how to work around this, because I never saw a need to do it.
Rick
Rick 2014 年 8 月 14 日
編集済み: Rick 2014 年 8 月 14 日
but look at the problem in the PDF, I don't think I can deliberately add another line to the argument list. Also, we can't just make it an anonymous function, how would you do it if it was a regular m-file? Pretty much if you had the test in front of you with, what would you put on those dotted lines??
Star Strider
Star Strider 2014 年 8 月 14 日
You could neglect to include t in the argument list:
ydot = InvPend1(y,g,L,k1,k2);
but your f function would still have to be:
f = @(t,y) InvPend1(y,g,L,k1,k2);
Rick
Rick 2014 年 8 月 14 日
I got it to work, by the way, the m-file doesnt have to have t as in input argument and it will still run fine
Star Strider
Star Strider 2014 年 8 月 14 日
Great!
Correct. Only the call to ode45 needs to.

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2014 年 8 月 14 日

コメント済み:

2014 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by