No Description of Error for ode45
Show older comments
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
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
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations 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!