Dear Matlab Community,
While doing a solution with MATLAB, I got 2 nonlinear equations, say, eqn1 and eqn2 generated with two variables s and t in output. The size of equations are large (got as output). How can I use "fsolve" for solving them without copying them from the output and without setting x(1) and x(2) but defining and ?

 Réponse acceptée

Having the equations themselves would hellp.
However assuming they are something like this:
y1 = s.^2 + 10*t;
y2 = 10*s + t.^1/3;
I would code them as:
y1 = @(s,t) s.^2 + 10*t;
y2 = @(s,t) 10*s + t.^1/3;
then:
X = fsolve(@(x) [y1(x(1),x(2)); y2(x(1),x(2))], [3 9])
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
X = 1×2
1.0e+-13 * -0.0099 0.2960
.

4 commentaires

Aniket Roy
Aniket Roy le 1 Juin 2021
Dear Sir,
Thank you so much. But then the problem is, I need to copy the equations (like yours y1, y2) from the output that already generated as eq1 and eq2. And every time I change any value of any parameter then every time I need to copy the equations because of their changing of shape. Is there sir, any way that I can call the equations, like y1=@(s,t) eq1(s,t) and y2=@ eq2(s,t); without copying or writing?
Thank you so much.
I have no idea what your equations are.
If they have extra parameters that need to be changed, add them to the argument list and pass those parameters in the funciton call. See Passing Extra Parameters for details.
Extending the previous example (adding parameter ‘z’) —
y1 = @(s,t,z) s.^2 + 10*t + 10*z;
y2 = @(s,t,z) 10*s + t.^1/3 + sqrt(z);
z = 42;
X = fsolve(@(x) [y1(x(1),x(2),z); y2(x(1),x(2),z)], [3 9])
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
X = 1×2
0.7538 -42.0568
So here ‘z’ can be assigned anywhere in the code prior to the fsolve call (where it is first used), and can change any time, or even in a loop to get different results for each value of ‘z’ that are changed in the loop.
.
Aniket Roy
Aniket Roy le 9 Juin 2021
Dear Sir,
thanks. I have got the equations and in script from and . And I got the full equations like
that I need to copy from the output and paste it in the script to use fsolve. Is there any way that I can directly use
y1=@(s,t) real(det(A)) and y2=@(s,t) imag(det(A))?
Star Strider
Star Strider le 9 Juin 2021
I need either to have the actual equations, or the information necessary to calculate ‘y1’ and ‘y2’.
I cannot go further without that information.
Also, what variables are they to be solved for?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by