## fsolve: How can I adress my variables to x(1)?

### Diego R (view profile)

on 24 Aug 2019 at 13:11
Latest activity Commented on by Walter Roberson

### Walter Roberson (view profile)

on 25 Aug 2019 at 21:58
Hello, everybody,
In my program I need to use fsolve, but don't know how to adress my variables to the x(1), x(2), etc. that needs fsolver.
I give you simplified example:
My program in a certain pointt gives me (x, y are variables):
y = 2*x
y = x^4
and I want to receive the solution: [2,2]
I can get if I do this:
----------------
function F=myfun(x)
F = [x(2)-x(1)^2 ; x(2)-2*x(1)];
end
fsolve(@myfun,[3,3])
--------------------------
But I can't do manually because my program has many and very complex equations. I need a way to adress my equations to the fsolve form.

Walter Roberson

### Walter Roberson (view profile)

on 24 Aug 2019 at 18:06
Would you happen to have the Symbolic Toolbox? It often makes this kind of task easier.

### Matt J (view profile)

on 24 Aug 2019 at 13:39
Edited by Matt J

### Matt J (view profile)

on 24 Aug 2019 at 13:44

I don't understand your question very well, but maybe it will help to mention that fsolve does not require you to express the function in terms of individual x(i). fsolve does not know or care how the function is expressed in code. It only cares that the function is differentiable.
For example, I could conceivably solve a linear system of equations as below, which uses no x(i) references at all:
A=rand(5); b=A*[1;2;3;4;5]
x=fsolve(@(x) A*x-b, ones(5,1))
x =
1.000000000000000
1.999999998886354
2.999999996241878
4.000000002442892
5.000000000534329

Matt J

### Matt J (view profile)

on 24 Aug 2019 at 13:40
Of course, you would never actually use fsolve to solve a linear system. Matlab has better solvers for that.

### Diego R (view profile)

on 25 Aug 2019 at 7:37
Edited by Diego R

### Diego R (view profile)

on 25 Aug 2019 at 8:08

I'll back to my example. How do you implement this (without writing it manually):
At a certain point, my program gives me this output (x,y are symbolic):
eqn1 = y == 2*x
eqn2 = y == x^2
What lines should I add to the code to get this solution?
x = 2
y = 4
edit:clarify example

Show 1 older comment
Diego R

### Diego R (view profile)

on 25 Aug 2019 at 8:23
But don't I need fsolve to give conditions?
In this example I only want the right side solution [2,4] and not [0,0], so I put the [3,3] condition. In my real program i have a piecewise function with many stretchs and several solutions. But I know in which stretch I have to search.
This is the reason of using fsolve.
Matt J

### Matt J (view profile)

on 25 Aug 2019 at 16:01
But I know in which stretch I have to search....This is the reason of using fsolve.
That's not the right reason to choose fsolve over the symbolic solver. The reason why a non-symbolic solver might be preferable for you is that you said you had many complicated equations. That makes it less likely that a symbolic solution exists.
If you want to try to solve symbolically, you could use the assume() command to specify bounds.
To solve non-symbolically, you should not use fsolve, but rather lsqnonlin, which has input arguments for specifying bounds on the variables. If your question is about how to convert symbolic equations to a form usable by non-symbolic solvers, you should use matlabFunction().
Walter Roberson

### Walter Roberson (view profile)

on 25 Aug 2019 at 21:58
solve() does not always respect assumptions.
fsolve() does not permit bounds to be specified. fzero() does permit bounds to be specified, but only supports a single function of a single variable.
If I recall correctly, fsolve() and fzero() assume continuity, which could be a problem with piecewise functions.
vpasolve() permits bounds to be specified for each variable.
>> solve(y==2*x, y==x^2,x>0)
ans =
struct with fields:
x: [1×1 sym]
y: [1×1 sym]
>> [ans.x,ans.y]
ans =
[ 2, 4]