Transforming a transfer function into a differential equation in Matlab - matlab

I have the following code in matlab:
syms s
num = [2.4e8];
den = [1 72 90^2];
hs = poly2sym(num, s)/poly2sym(den, s);
hs
f = ilaplace(hs)
The inverse Laplace transform converts the transfer function in the "s" domain to the time domain.I want to know if there is a way to transform the s-domain equation to a differential equation with derivatives. The following figure is an example:
I'm trying to implement a dynamic system in an s-function and I need to put it in this format to be able to access the system states.
I tried to work with the state space of this system but it's giving an error that I couldn't solve, I even have an unanswered question here, about this: "S-Function error message: Output returned by S-function 'Hi_plant_sfcn' in 'untitled/S-Function' during flag=3 call must be a real vector of length 1".
If anyone knows how to do this, I appreciate it.

Related

Plot solutions of ODEs with varying initial conditions

I am trying to solve a system of 2 differential equations with 2 variables. I then want to plot x(t) vs t for various initial conditions, and then do the same for y(t) vs t for various initial conditions. I'm struggling with doing this though, for some reason the MATLAB syntax is not clicking with me at all.
Here is the system of differential equations that I am trying to solve.
function dpdt = lODE(t,p)
dpdt = [-3*p(1) + 2*p(2);
-p(1) + (-1 + -2*p(2))];
end
Any help would be appreciated.
Edit: Here's some more context, and my actual code.
syms y(t) z(t);
eqns = [diff(y,t)==-3*y+2*z, diff(z,t)==y-2*z];
[ySol(t),zSol(t)] = dsolve(eqns);
This outputs the solution as a symfunc, which means I can't use it in PLOT. Again, my goal is to plot one of the solutions versus time.

Using Matlab to solve a system of ODEs using Euler's method

I have created a function Euler.m to solve a a system of ODEs using Euler's method. I wish to use this function to solve the system of ODEs defined by the anonymous function func=#(t) ([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)]) with initial conditions given by y0.
func=#(t) ([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)]);
y0=[4;5/4];
y_exact=#(t) [4*exp(3*t)+2*exp(-t)-2*exp(t);2*exp(3*t)-exp(-t)+exp(t)/4]; %exact solution of ODEs
a=0; % such that
b=1; % a<t<b
N=120;
[t,y] = Euler(func,a,b,y0,N)
However, the following error is displayed:
"Error using solution>#(t)([x(t)+4*y(t)-exp(t);x(t)+y(t)+2*exp(t)])
Too many input arguments.
Error in solution (line 7)
[t,y] = Euler(func,a,b,y0,N)".
Why is this error being displayed?
You are pretending that you already know when writing the ODE function func what the solutions x(t),y(t) are. Then you are going to compute solutions approximations for it. This is completely the wrong way around.
The function for the right side is just for a point in phase space, so you need
func=#(t,y) ([y(1)+4*y(2)-exp(t);y(1)+y(2)+2*exp(t)]);
where the input y is a two-component vector.

How to set up solving of multiple ODEs in Matlab propperly?

I have a task of writing an M script that would set up (and finally solve) a set of 19 differential equations and all of the equation coefficients. I am not sure what is the best way to input those equations.
Example of some equations:
Example of coefficients:
I don't think that this would be suited for Simulink. Other examples use a function in the form of #(t,x) where t is time and x the vector of all variables.
There are loads of "examples" online, but they don't seem to be appropriate for such a large set of large equations.
For example this exmple of solving 3 equatons
Even for 3 simple equations as they have solved, the functions are getting messy:
f = #(t,x) [-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
Using this notation and getting to the x(19) and cross-referencing all instances of x would be a mess.
I would like your help, and a simple example of how could I write these equations line by line, maybe using the symbolic toolbox, and then put them in an array that I can then forward to the solver.
As I said, I know there are examples online, but they tackle only the most basic system, and really they don't scale well if you want a clean and easily readable code.
I would like to have a similar to Wolfram alpha, where you type variable names as they are (instead od x(1), x(2), ... m x(19)) and if it's possible to get all solution vectors with their variable names.
You don't have to use an anonymous function handle as an ode function, you can create a separate function file (as shown in the odefun section of ode45.
For example, your odefun can look like:
function dy = myode(t,y)
% first unpack state variables
i_d2 = y(1);
i_q2 = y(2);
...
gamma2 = y(end-1);
omega2 = y(end);
% determine all constants
c34 = expression_for_c34;
...
c61 = expression_for_61;
% determine state derivative
i_d2_dot = expression;
...
omega2_dot = expression;
% pack state derivative based on order in state vector
dy(1) = i_d2_dot;
...
dy(end) = omega2_dot;
end
From this myode function you can also call other functions to e.g. determine the value for some coefficients based on the current state. Next, integrate the system using a suitable ode solver:
[t,y] = ode45(#myode,tspan,y0);

Converting symbolic equation to fitness function which can be evaluated by genetic algorithm

I have a symbolic equation and wish to convert to a function which can be evaluated by genetic algorithm (ga). I have tried using the matlabFunction and convert the symbolic equation into a matlab file. However, this generated file can only be evaluated by fmincon or patternsearch algorithms and not genetic algorithm. I get this error using ga.
Caused by: Failure in initial user-supplied fitness function evaluation. GA cannot continue.
It seems like the matlabFunction does not generate the format required by ga, can anyone please advise what's the solution/workaround to this problem?
The code are as follow:
N = 24;
X = sym('x',[2*N 1]);
Y = X(1:N);
W = 3.2516e-6.*Y.^3 - 0.0010074.*Y.^2 + 0.390950.*Y+2.2353;
Z = P.*W;
totR = sum(Z);
totR = subs(totR,[P],[price]);
matlabFunction(totR,'vars',{X},'file','objFcn');
% Call to ga
x1 = ga(#objFcn, N*2, A, b, Aeq, beq)
Thanks!
From the documentation for ga:
The fitness function should accept a row vector of length nvars and return a scalar value.
Your objFcn is accepts a column vector and throws an error if a row vector is passed in. You can fix this (I think) by changing this line to:
X = sym('x',[1 2*N]);
If P is non-scalar then you may need to transpose it. Of course, without runnable code there could be all sorts of other things going on too. There are probably other places and ways that you could fix the issue.

Partial derivative with Matlab Symbolic Toolbox for Lagrangian equations of motion

I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.
So I would do this in Matlab:
syms t x(t) % t: time, x(t) position dependent on time
m = sym('m'); % mass, a constant parameter
T = m/2*diff(x,t)^2; % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);
But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:
Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
Does anyone know the proper way of handling this?
Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.