How can I solve a set of equations in Simulink?

时间:2016-10-20 13:15:02

标签: matlab simulink equation symbolic-math

I need to solve a set of three equations, for which there are two solutions. As 'syms' will not work in a Simulink library block, I created an .m function which solves the equations. However, when I try to call this function using coder.extrinsic, I get the error 'Calls to coder.extrinsic may only appear at the top-level.', which I assume is due to the fact that the call is within an 'if' loop.

I can't move the call to the top-level, as I only define the variables for the equations in the 'else' part of the loop. This else loop is also part of another if loop. I have shown the .m function below. The variables, OMX,OMY,CX,CY,Target(1) and Target(2) are different depending on the path which the code takes.

Is there another way around this?

function [c] = Planner_Circles
% Create set of equations to define possible tangent circles between defined circle and target
syms xt yt rt; 
eqn1 = (xt-OMX)^2 + (yt-OMY)^2 == (rt-OMR)^2;
eqn2 = (xt-CX)^2 + (yt-CY)^2 == (rt+R)^2;
eqn3 = (xt-Target(1))^2 + (yt-Target(2))^2 == rt^2;
sol = vpasolve([eqn1, eqn2, eqn3], [xt, yt, rt]);
xSol = sol.xt;
ySol = sol.yt;
rSol = sol.rt; 
% This gives two solutions
rt=((rSol(1)));                                                            
xt=((xSol(1)));
yt=((ySol(1)));
rt2=((rSol(2)));
xt2=((xSol(2)));
yt2=((ySol(2)));   
% Choose largest radius for minimal curvature 
if rt<rt2;    
rt=rt2;
xt=xt2;
yt=yt2;

0 个答案:

没有答案
相关问题