MATLAB Answers

matlab error ode45

42 views (last 30 days)
merv sabar
merv sabar on 2 Dec 2020
Commented: merv sabar on 5 Dec 2020
i wrote this code, but i have error i dont know why !
x0=[0,10,45]; %initial condtion
tspan=[0,30];% time span
u=10;
[t,x]=ode45(@rolling,tspan,x0,[],u);
subplot(2,2,1)
plot(t,x(:,1))
ylabel('lamda (deg)')
subplot(2,2,2)
plot(t,x(:,2))
ylabel('P (deg/sec)')
subplot(2,2,3)
plot(t,x(:,3))
ylabel('phi (deg)')
subplot(2,2,4)
plot(t,x(:,4))
ylabel('Voltage (V)')
the error is:
Error using odearguments (line 93)
ROLLING must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in problem2_b (line 10)
[t,x]=ode45(@rolling,tspan,x0,[],u);

  0 Comments

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 2 Dec 2020
If you call your rolling function with the first element of tspan and the initial condition vector x0 as inputs, do you receive a column vector, a row vector, or something that isn't a vector at all?
q = rolling(tspan(1), x0)
From the error you received we can rule out it returning a column vector. If it's returning a row vector, either preallocate the output argument as a column vector and fill it in or take the non-conjugate transpose (the .' operator) of the output inside rolling immediately before rolling returns.

  3 Comments

merv sabar
merv sabar on 3 Dec 2020
thank you , but i dont have an inial values of X0
i just found the control gain and i have plot it with time for 30 sec
that why i called rolling function maybe its wrong , or how i have plot control gain with time ?
Walter Roberson
Walter Roberson on 3 Dec 2020
You do have initial values of x0:
x0=[0,10,45]
What exact output do you get when you call
rolling(0, x0)
?
merv sabar
merv sabar on 3 Dec 2020
so i have use not two optin as output ([t,x]=ode45(@rolling,tspan,x0,[],u);) i have used just y = (tspan,x0) ?

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Dec 2020
x0=[0,10,45]; %initial condtion
tspan=[0,30];% time span
u=10;
odefun = @(t,y)reshape(rolling(t,y,u), [], 1);
[t,x]=ode45(odefun, tspan, x0);
subplot(2,2,1)
plot(t,x(:,1))
ylabel('lamda (deg)')
subplot(2,2,2)
plot(t,x(:,2))
ylabel('P (deg/sec)')
subplot(2,2,3)
plot(t,x(:,3))
ylabel('phi (deg)')
subplot(2,2,4)
plot(t,x(:,4))
ylabel('Voltage (V)')
You were using an obsolete syntax to pass in an extra parameter to your function. That syntax has not been documented for a long enough time that I was not able to find a copy of the documentation old enough to show that it ever existed -- pretty much 20 years obsolete.
Secondly, your function rolling has a bug. It has probably been written with code similar to
dy(1) = something
dy(2) = something
dy(3) = something
This is wrong because when you have a scalar object, and you use a single subscript to assign to a location past the end of the scalar, then the output is a row vector -- but your ode function must return a column vector. Rather than going back and forth with you to show you exactly which line in your function you have to change, I use the anonymous function to force the output to be a column vector instead of a row vector.

  3 Comments

merv sabar
merv sabar on 5 Dec 2020
this is my rolling function:
function dy =rolling(t,x,u)
dy=zeros(3,1); %dy=[ ]%%function output_variable=function_name(time,states, inputs)-standardx
A=[-10,0,0;30,-1,0;0,1,0];
B=[10,0,0];
dy=A*x+B*u;
Walter Roberson
Walter Roberson on 5 Dec 2020
Your x will be received as a 3x1 column vector. Your u is scalar
(3x3) * (3x1) is valid and gives a 3x1 result.
(1x3) * (1x1) is valid and gives a 1x3 result.
(3x1) + (1x3) is valid since R2016b and gives a 3x3 result.
You assign the 3x3 over top of all of the output variable, so whatever you had assigned to the variable before is irrelevant.
Could the code have been written expecting a row vector for x?
(3x3) * (1x3) is invalid.
So, no, the code is not expecting row vector.
The code as written would require that u is 3x1 so that 1x3 * 3x1 would give 1x1 to be added to the 3x1
merv sabar
merv sabar on 5 Dec 2020
yes i fix it
thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by