Problem Calling my own Function in my Code

12 ビュー (過去 30 日間)
Hannah Pike
Hannah Pike 2021 年 3 月 27 日
回答済み: Walter Roberson 2021 年 3 月 27 日
I am trying to call a function in my code and I keep getting an error message that reads ...
"Error using odearguments (line 95)@(T,Q)NONLINEAR2(W,Q,T) returns a vector of length 1, but the length of initial conditions vector is 2.
The vector returned by @(T,Q)NONLINEAR2(W,Q,T) and the initial conditions vector must have the same
number of elements."
I am confused on what this means and how I go about fixing it. If you could help me fix my function call in my code that would be amazing! I will post my code below as well as the function.
Main Code:
tspan = [0 80]; % time
qinit = [0 ; 1]; % q initial
for w = [0, 0.5, 1, 2, 4, 8, 16] % w (calculates the IVP solution as w changes)
[t,q] = ode45(@(t,q) nonlinear2(w,q,t),tspan,qinit); % function call; % function call
figure(1)
plot(t,q(:,1),'-') % plots IVP Solution vs time
grid on % adds a grid
xlabel("Time") % x-axis label (time)
ylabel("IVP Solutiion") % y-axis label (IVP Solution)
title("IVP Solution vs Time (Question 3)") % Adds a title to the plot
hold on
end
Function :
function qp = nonlinear2(w,q,t) % defines function and variables needed
qp = [-4* q(1) + 5 * q(2) + 10 * cos(w * t)];
end

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 27 日
You have two boundary conditions, such as for q and q'. You need to return one entry for each input boundary condition.
If your equation is of the form
q''(t) = function in q'(t) and q(t)
then you would (typically) use the q(t) and q'(t) boundary conditions to calculate the current q''(t) and return it as the second entry in the column vector, and you would also in that case set the first entry as dp(1) = q(2) implying that q'(t) is the integral of q''(t). But remember, column vector!

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by