フィルターのクリア

How do I turn my function into a column vector?

2 ビュー (過去 30 日間)
Wouter Meekes
Wouter Meekes 2019 年 1 月 13 日
コメント済み: madhan ravi 2019 年 1 月 13 日
I'm trying to solve a second-order differential equation using matlab, but for some reason I keep getting the error message that my function should be a column vector.
I'm calling the function (tryagain) from the following script (wilyourunnow):
clear all %Remove stray stuff
ht = linspace(0,10,1001);
h = sin(ht);
%This is based on the instructions as given in the help browser on the page
%for ode45; tab ODE with Time-Dependent Terms
[t,x] = ode45(@(t,x)'tryagain', (0:0.01:10), [0 1]);
plot(t,x)
And this is the function:
function dxdt = tryagain(t,x,ht,h)
h=interp1(ht,h,t);
dxdt = zeros(2, 1);
dxdt(1)= x(2);
dxdt(2) = -x.^2 + x + h;
When I try to run the script, I get the following error:
>> wilyourunnow
Error using odearguments (line 93)
@(T,X)'TRYAGAIN' must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in wilyourunnow (line 10)
[t,x] = ode45(@(t,x)'tryagain', (0:0.01:10), [0 1]);
I've already tried transposing the vector with these two commands (separately)
dxdt = transpose(dxdt)
dxdt = dxdt(:)

採用された回答

madhan ravi
madhan ravi 2019 年 1 月 13 日
編集済み: madhan ravi 2019 年 1 月 13 日
ht = linspace(0,10,1001);
h = sin(ht);
%This is based on the instructions as given in the help browser on the page
%for ode45; tab ODE with Time-Dependent Terms
[t,x] = ode45(@(t,x) tryagain(t,x,ht,h), (0:0.01:10), [0 1]); % change to be noted
plot(t,x)
function dxdt = tryagain(t,x,ht,h)
h=interp1(ht,h,t);
dxdt = zeros(2, 1);
dxdt(1)= x(2);
dxdt(2) = -x(2).^2 + x(1) + h; % two x here one has to be x(1) and the other x(2) , you have to correct it according to your equation parameters
end
Gives:
  4 件のコメント
Wouter Meekes
Wouter Meekes 2019 年 1 月 13 日
I realised where it went wrong only after literally copy-pasting your code over my own. When trying to solve the problem myself, I'd tried transposing dxdt (once more after posting the question), but forgot that I input that line. Removing that worked, and now with x replaced by x(1) all is solved.
Thanks a lot!
madhan ravi
madhan ravi 2019 年 1 月 13 日
Anytime :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by