Solving non linear equations using fsolve

1 Ansicht (letzte 30 Tage)
siddartha tadepalli
siddartha tadepalli am 25 Feb. 2019
Kommentiert: John D'Errico am 26 Feb. 2019
I am trying to solve a sample code using fsolve.
syms t;
syms x y;
eqn=[2^x-y;
y-sin(t)];
time=pi/2;
eqn1=subs(eqn,t,time);
out=matlabFunction(eqn1);
soln=fsolve(out,[2,0]');
Can someone help me out?

Akzeptierte Antwort

John D'Errico
John D'Errico am 26 Feb. 2019
Bearbeitet: John D'Errico am 26 Feb. 2019
Using the symbolic toolbox is often a mistake that people do, just because they think because the value of some parameters is not currently known, then they MUST be symbolic. It seems a logical conclusion. But it is not true. When you create/write a function of some parameters, you need not in advance know exactly what values those paramters will take on.
You have some function, with unknown parameters.
Exyt = @(x,y,t) [2.^x - y;y - sin(t)];
It exists. It is definable, and usable. If you know the value of those parameters, you can evaluate those expressions. And if you know the value of SOME of them, you can fix them at some point in time. Thus:
Exyt(1,2,3)
ans =
0
1.8589
We can now create a new function handle, fixing t at any value we desire.
Exy = @(x,y) Exyt(x,y,pi/4)
Exy =
function_handle with value:
@(x,y)Exyt(x,y,pi/4)
Exy(2,3)
ans =
1
2.2929
It can be passed around, used by tools such as fsolve.
[xy,fval,exitflag] = fsolve(@(xy) Exy(xy(1),xy(2)),[1 1])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
xy =
-0.5 0.70711
fval =
2.3215e-12
0
exitflag =
1
So for t = pi/4, the solution is as given. Nothing ever needed to be declared as symbolic at any point in time.
In fact, we can even get fancy. Suppose we wanted to know the solution in terms of x and y, as an implicit function of t? NOT A PROBLEM. We will return to Exyt for this.
opts = optimset('fsolve');
opts.Display = 'none';
xyoft = @(t) fsolve(@(xy) Exyt(xy(1),xy(2),t),[1 1],opts);
xyoft(pi/3)
ans =
-0.20752 0.86603
xyoft(pi/4)
ans =
-0.5 0.70711
Again, nothing symbolic ever passed through my fingers. There was never any need for that to happen.
I could also have varied t in a loop, creating a new function handle on the fly as I needed it, each time with that value of t embedded inside of it, much as I created xyoft.
COULD I have used the symbolic toolbox? Of course. It would be slower. A more inefficient use of CPU cycles. But if the plan is to use fsolve anyway, then why would I do so? Just because you have a hammer, this should not make every problem look like a nail.
  2 Kommentare
John D'Errico
John D'Errico am 26 Feb. 2019
Please don't make a comment using an answer.
"I am really thakful for your insight on to the problem John.
But,I need to diiferneciate these expressions later for velocities and accelarations.
This is just a sample code I was trying to get acustomed to these functions.
Having a symbolic variable would help me greatly in the subsequent calculations. So, how can I get the same output using symbolic variables, being ready to compromise on the speed of computation?"
John D'Errico
John D'Errico am 26 Feb. 2019
Sigh. I'd bet you still did not need to use syms. But if you insist, the answer is trivial.
syms x y;
eqn=[2^x-y;
y-sin(t)];
time=pi/2;
eqn1=subs(eqn,t,time);
out=matlabFunction(eqn1);
>> out
out =
function_handle with value:
@(x,y)[-y+2.0.^x;y-1.0]
So out is a function of TWO variables. Fsolve needs to see this as a vector of length 2, NOT two distinct variables.
fsolve(@(xy) out(xy(1),xy(2)),[2 0])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
5.3718e-08 1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 25 Feb. 2019
t=pi/2;
eqn=@(x,y) [2^x-y;
y-sin(t)];
[p,fval]=fsolve(@(p) eqn(p(1),p(2)),[2,0].')
  1 Kommentar
siddartha tadepalli
siddartha tadepalli am 26 Feb. 2019
Bearbeitet: siddartha tadepalli am 26 Feb. 2019
But, I need sym variable for further calculations.
Can I solve the same using symbolic toolbox?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by