How do I use ode45 properly? (Syntax related)

1 view (last 30 days)
JohnnyBoy
JohnnyBoy on 16 Nov 2015
Commented: Star Strider on 16 Nov 2015
I have a state-space representation of a control system I'm working on for school. I have the following matrices:
A = [0 1 0 0;(w0^2) (2*d*w0) (-w0^2) (-2*d*w0);0 0 0 1;(w1^2) (2*p*w1) (-w1^2) (-2*p*w1)];
B = [0;(1/Mt);0;0];
C = [1 0 -1 0];
D = [0];
Variables are defined:
T = 2700;
Mt = 12000;
Mr = Mt;
r = 0.5;
F = T/r;
k = 10000;
b = 10000;
iniCon = [0;0;0.3;0];
w0 = sqrt(k/Mt);
d = b/(2*sqrt(k*Mt));
w1 = sqrt(k/Mr);
p = b/(2*sqrt(k*Mr));
t = 0:0.2:10;
All variables are previously defined (no symbolic). I wish to use ode45 to solve and plot the system. Here is the time span and initial conditions that I have and the line for ode45 which I am unsure of the syntax.
tspan = [0 10];
iniCon = [0 0 0.3 0]; %x3 = 0.3 and x1 = 0. I'm looking for the difference between x1 and x3
[t, y] = ode45([A B C D], tspan, iniCon);
plot(t, y)
The code returns this error message:
Error using horzcat Dimensions of matrices being concatenated are not consistent.
How can I fix my syntax? I need to plot a step response.
Thanks all

Answers (2)

James Tursa
James Tursa on 16 Nov 2015
The first argument to ode45 is supposed to be your derivative function. You are attempting to pass in a matrix in this spot (and one that is improperly built). What is the derivative function for your system?
  1 Comment
JohnnyBoy
JohnnyBoy on 16 Nov 2015
Really what I'm trying to do is plot the step response but with initial conditions. This is a spring damper system of 2 unrestrained masses (only k and b between the two). The second row of the matrix A is the first equation and the fourth is the second equation. I am looking for X1-X3.

Sign in to comment.


Star Strider
Star Strider on 16 Nov 2015
In a control problem such as you are doing, you only integrate the ‘A’ matrix, not the entire system.
However, it is not necessary to use ode45 at all (unless you were told to use it). For a linear problem such as yours appears to be, I would use the matrix exponential function expm. You will have to use it in a for loop to create expm(A*t), but after that you can use the result of the Laplace transform and its inverse of the system: y=C*expm(A*t)*B*u (since your ‘D’ matrix is 0) at each time step to solve your system. You will have to experiment with this to understand how it works, but it is not difficult. There are probably a number of examples using that technique in MATLAB Answers if you search for them.
  2 Comments
JohnnyBoy
JohnnyBoy on 16 Nov 2015
I was told to use ode45 by my professor. All I really need help with is how to format it to be input in the function.
Star Strider
Star Strider on 16 Nov 2015
What you need to do with the ‘A’ matrix is to multiply it by ‘x’ to satisify part of the equation for the states:
dx = A*x + B*u
so the anonymous function for it becomes:
Amtx = @(t, x, d, p, w0, w1) [0 1 0 0; (w0^2) (2*d*w0) (-w0^2) (-2*d*w0); 0 0 0 1; (w1^2) (2*p*w1) (-w1^2) (-2*p*w1)]*[x(1); x(2); x(3); x(4)];
and after you’ve put all your other variables into your workspace, the ode45 call becomes:
[t,x] = ode45(@(t,x) Amtx(t, x, d, p, w0, w1), t, iniCon);
That works. (I tested it.) The rest I leave to you!
It’s not necessary to include all the parameters in the ‘Amtx’ function declaration because it will pick them up from the workspace if you define the function after the variables are defined. I include them because I know the function will pick them up even if I define them after I define the function.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!