Simulink integration with variable bounds - matlab

enter image description here
I need to implement a integral in my simulink model with a variable upper bound. The upper bound is a signal in my model which gets smaller every step.
I tried to put the variable in the workspace (simout-block) and solve the integral in matlab (syma sigma tf; f = .....; int(f, t0, tf);). Then i tried to take the solved integral with simin-block.
Im new to the simulink thing. Would this proceed will be the right for my problem? Im not sure if the integral will get solved for every step.
Besides that: i get a error for my simin-block because the input format of my solved integral is not supported.

Related

Transforming a transfer function into a differential equation in 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.

Vectorised Function in numjac MatLab

I am trying to solve a large DAE system, coupled with the equations to calculate the sensitivity of the variables to a parameter. My problem is the jacobian of the entire system, its calculation is pretty slow and I would like to speed it up.
I am using numjac, in this form:
[Jx,FAC,G] = numjac(#(t,y)MODEL(t,y,X),tt,yy,dydt,jac_tol,FAC,0,JPat,G);
I want to vectorize the code, but I can't seem to get what this means. As far as I understood my code is already vectorized! t,y,X go in and I get a column vector of dy(i)/dt, or F(t,y(i)). But if I say that my function is vectorized, I get an error:
Matrix dimensions must agree.
Error in numjac (line 192)
Fdiff = Fdel - Fty(:,ones(1,ng));
How can I properly vectorize it?

Matlab ode solver algebreic calculation

I have an ODE model in Matlab coded as a function. I integrate the ODE's over time using ode15s. After the solver has finished, I calculate algebraically, a new variable that's a fraction of the sum of the other variables output by the model.
For example:
dA/dt = xxxxx
dB/dt = xxxxx
dC/dt = xxxxx
this gets integrated and returns an array of columns for dA,dB,dC. From the output I calculate:
model_result = A/(A+B+C).
for the whole time course), and plot this vector of values vs time.
However, I also need to perform sensitivity analysis on the model, for which I've used the modified ODE15s on the Mathworks repository. I need to be able to set "model_result" above as the objective function for the sensitivity calculations.
I attempted to calculate model_result inside the function, and have it returned as another variable, but obviously this doesn't work as the integrator returns the integrated value, rather than the algebraically calculated value.
Is it possible to have algebraic values returned by an ODE solver in Matlab?
If not, does anybody have any idea how to perform this kind of sensitivity analysis?
thanks for any help. I'm quite new to modelling so apologies if I'm using all the wrong terms!

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.

MATLAB: Plot integral using quad/quadl

I would like to know if anybody knows how I can plot an integral calculated using quad/quadl, or if this is possible.
I read that I can set the trace parameter to be non-zero, and this results in the information of each iteration being provided, but I'm not sure how and if I can use the information to plot an integral.
Thanks.
quad and quadl do not compute an integral function anyway, i.e., an integral as a function of the parameter. And since tools like this work iteratively, refining their estimate until it satisfies a tolerance on the global value, they are not easily made to produce the plot you desire.
You can do what you desire by using a differential equation solver to generate the solution, ode45 for example.