No Description of Error for ode45

1 回表示 (過去 30 日間)
Peyton Schroeder
Peyton Schroeder 2020 年 12 月 4 日
コメント済み: James Tursa 2020 年 12 月 4 日
I am trying to use the ode45 function to plot the response of the output y(t) for
where all inputs are step functions with initial conditions
and .
I think most of my code is correct, but I am having an issue with line 21. I am receiving this error message:
Error in Problem_7_A (line 21)
[t,x]=ode45(odefun,time_int, x0);
However, there is no description, so I am unsure what the error is and I don't know how to fix it.
My code is pasted below.
clc; % clear command window
syms x t; % create symbolic variables x and t
% define system parameters
sp.a=4; % coefficient on ydot term
sp.b=3; % coefficient on y term
sp.c=2; % coefficient on ycubed term
% define initial conditions
x0=zeros(2,1); % define matrix size
x0(1)=1; % initial position y(0)=x0(1)=1
x0(2)=0; % initial velocity ydot(0)=x0(2)=0
% define time interval for solution
tinitial=0; % set initial time
tfinal=10; % set final time
time_int=[tinitial tfinal]; % define time interval bounds
% call the ode45 function
[t,x]=ode45(odefun,time_int, x0);
% plot solution x(t)
figure(1); hold on; % create figure
plot(t,x(:,1),'LineWidth',2); % plot time on x axis and response on y axis
th=title('Output Response Using ode45'); % set title
xh=xlabel('$t$ [s]'); % set x axis label
yh=ylabel('$y(t)$'); % set y axis label
% customize fonts
set(th,'FontSize',15); % set font size for title
set(xh,'FontSize',15); % set font size for x axis
set(yh,'FontSize',15); % set font size for y axis
set(xh,'Interpreter','latex'); % interpret characters using LaTeX markup
set(yh,'Interpreter','latex'); % interpret characters using LaTeX markup
fh=gca; % get current axes
set(fh,'FontSize',15); % set font size for axes
function dx = odefun(t,x,sp)
% for readability, extract system parameters from sp
a=sp.a; % coefficient on ydot term
b=sp.a; % coefficient on y term
c=sp.c; % coefficient on ycubed term
u=t; % STILL NEED TO CHANGE TO STEP FUNCTION H(T)
dx=zeros(2,1); % initialize vector of states (here 1D)
x1=x(1);
x2=x(2);
% use state variable ODE to calculate dx
dx(1)=x2;
dx(2)=(-a*x2)+(-b*x1)+(-c*x1*x1*x1)+(-x2*x1*x1)+u;
end

採用された回答

James Tursa
James Tursa 2020 年 12 月 4 日
編集済み: James Tursa 2020 年 12 月 4 日
Looks like your odefun needs additional inputs that you are not providing. E.g., try changing this
[t,x]=ode45(odefun,time_int, x0);
to this
[t,x]=ode45(@(t,x)odefun(t,x,sp),time_int, x0); % also pass sp
Also, note that the following line accomplishes nothing and can be removed (you don't use syms variables for ode45):
syms x t; % create symbolic variables x and t
  2 件のコメント
Peyton Schroeder
Peyton Schroeder 2020 年 12 月 4 日
That fixed my problem! Thanks for your help. My only question is: why do we remove sp from that line? In some of the examples I have seen, sp is included there, but I will admit I don't fully understand the purpose of using system parameters.
James Tursa
James Tursa 2020 年 12 月 4 日
"... why do we remove sp from that line ..."
I don't understand. I added sp to the line in question ... I didn't remove it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by