matlab error ode45

12 ビュー (過去 30 日間)
merv sabar
merv sabar 2020 年 12 月 2 日
コメント済み: merv sabar 2020 年 12 月 5 日
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);

回答 (2 件)

Steven Lord
Steven Lord 2020 年 12 月 2 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 3 日
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 2020 年 12 月 3 日
編集済み: merv sabar 2020 年 12 月 3 日
so i have use not two optin as output ([t,x]=ode45(@rolling,tspan,x0,[],u);) i have used just y = (tspan,x0) ?

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


Walter Roberson
Walter Roberson 2020 年 12 月 3 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 5 日
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 2020 年 12 月 5 日
yes i fix it
thank you

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by