Using 'solve' function in Simulink

I have a very non-linear equation that I need to solve during run-time of a Simulink simulation. I have been using the 'solve' function in normal scripts to solve for this. Unfortunately, I need this to be solved in run-time of the simulation.
I have tried to use an embedded Matlab function with the 'solve' function in the script. The problem is that it doesn't like 'syms' command used in conjunction with the 'solve' command. I have tried using the coder.extrinsic with the parameter, but it gives the error, "Undefined function or variable 'v'.... It doesn't seem to appreciate that I have declared 'v' as a syms in order for v to be solved for in the 'solve' function.
Has anyone successfully got the 'solve' function to work in a user-defined Matlab block in Simulink? I have raised this issue in front of a few dozen students at my University, and am interested in showing them a solution. Thank you!
For your ease, I have attached the simulation I am attempting to run. You can find the specific problematic block in the 'Velocity' block on the 'Top Level' file.

1 Commento

To check: would it be possible for you to do the solve() symbolically in MATLAB, and use matlabFunction() to generate parameterized MATLAB code, that you incorporate into Simulink ?

Accedi per commentare.

 Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 13 Nov 2013

0 voti

Try Interpreted Matlab function block, formerly called Matlab function .

5 Commenti

Azzi, The equation is such that I am not able to solve for the variable algebraically.
P = .5*R*Cd*A*v^3 + Cr*M*g*v*cos(theta) + (M*g*v*sin(theta)
I have the inputs of every variable except for v. I am solving for v. Unfortunately, I haven't found an algebraic solver that can do this yet.
Can I put a 'solve' function in the interpreted Matlab function? I couldn't see a manner of getting that to work.
This was the correct thing to do! I finally figured out how to use that simulink block correctly. I created a matlab function script. The Interpreted MATLAB Fcn called that script, passing in the inputs. It returned the solution to the 'solve' command, and it runs very smoothly. Again, Thank you Azzi for helping me!
Mikkel
Mikkel il 9 Giu 2014
Modificato: Mikkel il 9 Giu 2014
How did you do this? I have a simmelar problem but cannot get it working using the interpreted matlab function.. ? I do not get an error message but it simply wont accept my equation into the block. hence no result
Mikkel: Please specify the line that you are entering on the block.
Kaustubha: I'm now entering this
eval(solve(u(2)*cos(u(3)-u(4))-u(1)*cos(x-u(4)),x))
and it is being calculated, though extremely slow! is there a way to speed this up?

Accedi per commentare.

Più risposte (1)

If
P = .5*R*Cd*A*v^3 + Cr*M*g*v*cos(theta) + (M*g*v*sin(theta)
then
R*Cd*A/2 * v^3 + M*g*(Cr*cos(theta) + sin(theta))*v - P = 0
the solution is then
roots([R*Cd*A/2, 0, M*g*(Cr*cos(theta) + sin(theta)), -P])
Two of the roots might be imaginary.

Community Treasure Hunt

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

Start Hunting!

Translated by