Problem Calling my own Function in my Code
12 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 27 Mar 2021
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!
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!