Solving a state-space (2nd order equation) with ode45 in MATLAB - matlab

I'm trying to teach myself how to use MATLAB for solving state-space systems, I have what seems to be a pretty straight-forward system but have been unable to find any decent straight-forward examples for a novice thus far.
I'd like a simple walk-through of how to translate the system into MATLAB, what variables to set, and how to solve for about 50(?) seconds (from t=0 to 50 or any value really).
I'd like to use ode45 since it's a 4th order method using a Runge-Kutta variant.
Here's the 2nd-order equation:
θ''+0.03|θ'|θ'+4pi^2*sinθ=0
The state-space:
x_1'=x_2
x_2'=-4pi^2*sin(x_1)-0.03|x_2|x_2
x_1 = θ, x_2 = θ'
θ(0)=pi/9 rad, θ'(0)=0, h(step)=1

You need a derivative function function, which, given the current state of the system and the current time, returns the derivative of all of the state variables. Generally this function is of the form
function xDash=derivative(t,x)
and xDash is a vector with the derivative of each element, and x is a vector of the state variables. If your variables are called x_1, x_2 etc. it's a good idea to put x_1 in x(1), etc. Then you need a formula for the derivative of each state variable in terms of the other state variables, for example you could have xDash_1=x_1-x_2 and you would code this as xDash(1)=x(1)-x(2). Hopefully that clears something up.
For your example, the derivative function will look like
function xDash=derivative(t,x)
xDash=zeros(2,1);
xDash(1)=x(2);
xDash(2)=-4*pi^2*sin(x(1))-0.03*abs(x(2))*x(2);
end
and you would solve the system using
[T,X]=ode45(#derivative,0:50,[pi/9 0]);
This gives output at t=0,1,2,...,50.

Related

Matlab ode15s: postive dx/dt, decreasing x(t)

In my script, I call the ODE solver ode15s which solves a system of 9 ODE's. A simplified structure of the code:
[t, x] = ode15s(#odefun,tini:tend,options)
...
function dx = odefun(t,x)
r1=... %rate equation 1, dependent on x(1) and x(3) for example
r2=... %rate equation 2
...
dx(1) = r1+r2-...
dx(2) = ...
...
dx(9) = ...
end
When reviewing the results I was curious why the profile of one state variable was increasing at a certain range. In order to investigate this, I used conditional debugging within the ode function so I could check all the rates and all the dx(i)/dt equations.
To my big surprise, I found out that the differential equation of the decreasing state variable was positive. So, I simulated multiple rounds with the F5-debug function, and noticed that indeed the state variable consistently decreased, while the dx(i)/dt would always remain positive.
Can anyone explain me how this is possible?
It is not advisable to pause the integration in the middle like that and examine the states and derivatives. ode15s does not simply step through the solution like a naive ODE solver. It makes a bunch of calls to the ODE function with semi-random states in order to compute higher-order derivatives. These states are not solutions to system but are used internally by ode15s to get a more accurate solution later.
If you want to get the derivative of your system at particular times, first compute the entire solution and then call your ODE function with slices of that solution at the times you are interested in.

For loop equation into Octave / Matlab code

I'm using octave 3.8.1 which works like matlab.
I have an array of thousands of values I've only included three groupings as an example below:
(amp1=0.2; freq1=3; phase1=1; is an example of one grouping)
t=0;
amp1=0.2; freq1=3; phase1=1; %1st grouping
amp2=1.4; freq2=2; phase2=1.7; %2nd grouping
amp3=0.8; freq3=5; phase3=1.5; %3rd grouping
The Octave / Matlab code below solves for Y so I can plug it back into the equation to check values along with calculating values not located in the array.
clear all
t=0;
Y=0;
a1=[.2,3,1;1.4,2,1.7;.8,5,1.5]
for kk=1:1:length(a1)
Y=Y+a1(kk,1)*cos ((a1(kk,2))*t+a1(kk,3))
kk
end
Y
PS: I'm not trying to solve for Y since it's already solved for I'm trying to solve for Phase
The formulas located below are used to calculate Phase but I'm not sure how to put it into a for loop that will work in an array of n groupings:
How would I write the equation / for loop for finding the phase if I want to find freq=2.5 and amp=.23 and the phase is unknown I've looked online and it may require writing non linear equations which I'm not sure how to convert what I'm trying to do into such an equation.
phase1_test=acos(Y/amp1-amp3*cos(2*freq3*pi*t+phase3)/amp1-amp2*cos(2*freq2*pi*t+phase2)/amp1)-2*freq1*pi*t
phase2_test=acos(Y/amp2-amp3*cos(2*freq3*pi*t+phase3)/amp2-amp1*cos(2*freq1*pi*t+phase1)/amp2)-2*freq2*pi*t
phase3_test=acos(Y/amp3-amp2*cos(2*freq2*pi*t+phase2)/amp3-amp1*cos(2*freq1*pi*t+phase1)/amp3)-2*freq2*pi*t
Image of formula below:
I would like to do a check / calculate phases if given a freq and amp values.
I know I have to do a for loop but how do I convert the phase equation into a for loop so it will work on n groupings in an array and calculate different values not found in the array?
Basically I would be given an array of n groupings and freq=2.5 and amp=.23 and use the formula to calculate phase. Note: freq will not always be in the array hence why I'm trying to calculate the phase using a formula.
Ok, I think I finally understand your question:
you are trying to find a set of phase1, phase2,..., phaseN, such that equations like the ones you describe are satisfied
You know how to find y, and you supply values for freq and amp.
In Matlab, such a problem would be solved using, for example fsolve, but let's look at your problem step by step.
For simplicity, let me re-write your equations for phase1, phase2, and phase3. For example, your first equation, the one for phase1, would read
amp1*cos(phase1 + 2 freq1 pi t) + amp2*cos(2 freq2 pi t + phase2) + amp3*cos(2 freq3 pi t + phase3) - y = 0
Note that ampX (X is a placeholder for 1, 2, 3) are given, pi is a constant, t is given via Y (I think), freqX are given.
Hence, you are, in fact, dealing with a non-linear vector equation of the form
F(phase) = 0
where F is a multi-dimensional (vector) function taking a multi-dimensional (vector) input variable phase (comprised of phase1, phase2,..., phaseN). And you are looking for the set of phaseX, where all of the components of your vector function F are zero. N.B. F is a shorthand for your functions. Therefore, the first component of F, called f1, for example, is
f1 = amp1*cos(phase1+...) + amp2*cos(phase2+...) + amp3*cos(phase3+...) - y = 0.
Hence, f1 is a one-dimensional function of phase1, phase2, and phase3.
The technical term for what you are trying to do is find a zero of a non-linear vector function, or find a solution of a non-linear vector function. In Matlab, there are different approaches.
For a one-dimensional function, you can use fzero, which is explained at http://www.mathworks.com/help/matlab/ref/fzero.html?refresh=true
For a multi-dimensional (vector) function as yours, I would look into using fsolve, which is part of Matlab's optimization toolbox (which means I don't know how to do this in Octave). The function fsolve is explained at http://www.mathworks.com/help/optim/ug/fsolve.html
If you know an approximate solution for your phases, you may also look into iterative, local methods.
In particular, I would recommend you look into the Newton's Method, which allows you to find a solution to your system of equations F. Wikipedia has a good explanation of Newton's Method at https://en.wikipedia.org/wiki/Newton%27s_method . Newton iterations are very simple to implement and you should find a lot of resources online. You will have to compute the derivative of your function F with respect to each of your variables phaseX, which is very simple to compute since you're only dealing with cos() functions. For starters, have a look at the one-dimensional Newton iteration method in Matlab at http://www.math.colostate.edu/~gerhard/classes/331/lab/newton.html .
Finally, if you want to dig deeper, I found a textbook on this topic from the society for industrial and applied math: https://www.siam.org/books/textbooks/fr16_book.pdf .
As you can see, this is a very large field; Newton's method should be able to help you out, though.
Good luck!

how to solve first order of system of PDEs in Matlab

I have a set of 4 PDEs:
du/dt + A(u) * du/dx = Q(u)
where,u is a matrix and contains:
u=[u1;u2;u3;u4]
and A is a 4*4 matrix. Q is 4*1. A and Q are function of u=[u1;u2;u3;u4].
But my questions are:
How can I solve above equation in MATLAB?
If I solved it by PDE functions of Matlab,can I convert it to a
simple function that is not used from ready functions of Matlab?
Is there any way that I calculate A and Q explicitly. I mean that in
every time step, I calculate A and Q from data of previous time step
and put new value in the equation that causes faster run of program?
PDEs require finite differences, finite elements, boundary elements, etc. You can also turn them into ODEs using transforms like Laplace, Fourier, etc. Solve those using ODE functions and then transform back. Neither one is trivial.
Your equation is a non-linear transient diffusion equation. It's a parabolic PDE.
The equation you posted has the additional difficulty of being non-linear, because both the A matrix and Q vector are functions of the independent variable q. You'll have to start by linearizing your equations. Solve for increments in u rather than u itself.
Once you've done that, discretize the du/dx term using finite differences, finite elements, or boundary elements. You should start with a weighted residual integral formulation.
You're almost done: Next to integrate w.r.t. time using the method of your choice.
It's not trivial.
Google found this: maybe it will help you.
http://www.mathworks.com/matlabcentral/fileexchange/3710-nonlinear-diffusion-toolbox

integration of function in modelica

I would like to perform integration of a function of a variable other than time in Modelica, but I don't know how to do it.
For example, how can I evaluate the integral of x dx with upper limit 5 and lower limit 2?
∫x dx=x^2/2
Modelica was not designed to be a CAS (computer algebra system) as Maple, Mathematica or Matlab, but with a little coding you can do it anyway. The thing is that your problem can not be solved automatically symbolically with Modelica tools, but numerically yes.
In order to solve it numerically you have to do the trick to substitute the x with the time variable since in Modelica you can perform derivatives and therefore integrals only with respect to time. Therefore you can create a signal source with the function you want to integrate and then use it as input of the Modelica.Blocks.Continuous.Integrator block, that implements this simple equation:
model Integrator
input Real u;
output Real y;
equation
der(y) = u;
end Integrator;
Finally if you send as input to this block zero for t<2 and t<5, then you should get in output the correct value of your integral between 2 and 5:
I hope this helps,
Marco

Plotting and finding roots of bessel functions

I am trying to plot roots of a function that is composed of multiple bessel functions being added and multiplied in Matlab. The equation is Jm(omega)*Ik(omega)+Im(omega)*Jk(omega) where Jm is the bessel function of the first kind of order m (besselj). Im is the modified bessel function of the first kind of order m (besseli). For each mode m=o,1,2,...and n=1,2,3... The frequency omega(mn) is the corresponding root of the listed equation. m=0,1,2 n-1,2,3,4. I need to solve the equation for the 12 roots. I am new to Matlab and this is a little out of my league. So far I have this code but I wasn't sure if I needed the variable omega in the script or not. I have also looked at other people's questions on the suject but didn't see any quite like this. The plots I have seen look nothing like mine which tells me I am probably wrong. Thanks for any help.
m=(0:2); k=(1:3); n=(1:4);
Jm=besselj(m,n');
Ik=besseli(k,n');
Jk=besselj(k,n');
Im=besseli(m,n');
g=Jm.*Ik+Im.*Jk
plot(g)
Plotting
besselj and besseli take what you call omega as their second parameter, so to plot your function you should try something like
m=0; k=1; omega=0:0.02:10;
Jm=besselj(m,omega);
Ik=besseli(k,omega);
Jk=besselj(k,omega);
Im=besseli(m,omega);
g=Jm.*Ik+Im.*Jk;
plot(omega,g);
hold all;
plot(omega,0,'k');
axis([min(omega) max(omega) -100 100]);
This shows you that for m=1, k=1 the first zeros are around 3.2, 6.3 and 9.4:
Finding the roots numerically
You could implement Halley's method for your function g, similar to how the roots of besselj are determined in the MatlabCentral file linked by Cheery.