Problem Calling my own Function in my Code
조회 수: 12 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!