sqrt(complex(x)) error occuring earlier than expected
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ondrej Zoufaly
le 7 Oct 2023
Réponse apportée : Walter Roberson
le 7 Oct 2023
I have a simple simscape model of an object moving on a perimeter of an ellipse. I managed to do this with two prismatic joints and using an ellipse equation I was able to to prescribe motion for the object. When the object hits the pole of an ellipse it should run into error because of sqrt(complex(x)) which is quite obvious. However, it runs into the problem way earlier and I don't know why.
The simulink model looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1505139/image.png)
and the ellipse equation:
function y = fcn(x)
a=1;
b=1;
y = sqrt((b^2)*(1-x^2/a^2));
The simulation should stop near x=-1, but it stops with the domain error in this value of x:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1505144/image.png)
This is the error:
An error occurred while running the simulation and the simulation was terminated
Caused by:
Domain error. To compute complex results from real x, use 'sqrt(complex(x))'.
Error in sqrt.m (line 13)
coder.internal.error('Coder:toolbox:ElFunDomainError',mfilename);
Error in 'vazba_elipsa/ellipse equation' (line 5)
Could anybody tell me where the problem is ? Thank you for your answers, Ondrej.
0 commentaires
Réponse acceptée
Walter Roberson
le 7 Oct 2023
It looks to me as if you might be using continuous time. As such, the ode solver is going to evaluate the function at a number of different trial positions in order to determine where the best direction to step is. If the maximum step size is too large, then it might try beyond 1 even if it does not intend to go in that direction, as it "feels out" what the slope is like in the area.
You can impose a smaller maximum step size -- but as long as you are using continuous time and not clipping x then you should expect that x beyond +/-1 will be attempted as part of determining the correct slope for ode purposes.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur General Applications dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!