f

#### how to solve one equation (two variables) with some constrains by using fsolve, or fmincon?

```Hi,

I want to solve one equation with 2 variables:
sin(a)/cos(a) = (2*sin(x) + sin(y))/(2*cos(x) + cos(y));
where: a is known, x,y belong to [-pi,pi].

Code:
f =  @(z)(2*sin(z(1)) + sin(z(2)))/(2*cos(z(1))+cos(z(2))) - tan(carrFra);
z0 = [0,0];  % initial value
lb = [-pi,-pi];
ub = [pi,pi];

Since I got only one equation and some constraints, so I thought fsolve would be better for this problem as it uses iteration methods.

Questions:
1), How to add some constraints (x,y belong to [-pi,pi]) to fsolve?
2), This equation needs to be solved many times, sometimes it shows "solutions not found".  why is that? (it should have a solution in theory);
3), I've tried fmincon because it has the function of adding the lower bound and upper bound, and the equation was modified as abs(f), to make it close to 0, but the solution isn't correct. So how to use fmincon in this case?

Any suggestion would be greatly appreciated, and thank you very much for your patients.

Yang
``` 0  Yang
12/22/2016 7:39:04 AM comp.soft-sys.matlab  211264 articles. 25 followers. 2 Replies 677 Views Similar Articles

[PageSpeed] 6

```"Yang" wrote in message <o3fvuo\$173\$1@newscl01ah.mathworks.com>...
> Hi,
>
> I want to solve one equation with 2 variables:
> sin(a)/cos(a) = (2*sin(x) + sin(y))/(2*cos(x) + cos(y));
> where: a is known, x,y belong to [-pi,pi].
>
> Code:
> f =  @(z)(2*sin(z(1)) + sin(z(2)))/(2*cos(z(1))+cos(z(2))) - tan(carrFra);
> z0 = [0,0];  % initial value
> lb = [-pi,-pi];
> ub = [pi,pi];
>
> Since I got only one equation and some constraints, so I thought fsolve would be better for this problem as it uses iteration methods.
>
> Questions:
> 1), How to add some constraints (x,y belong to [-pi,pi]) to fsolve?
> 2), This equation needs to be solved many times, sometimes it shows "solutions not found".  why is that? (it should have a solution in theory);
> 3), I've tried fmincon because it has the function of adding the lower bound and upper bound, and the equation was modified as abs(f), to make it close to 0, but the solution isn't correct. So how to use fmincon in this case?
>
> Any suggestion would be greatly appreciated, and thank you very much for your patients.
>
>
> Yang
>
Since you've got one equation and two variables you'll most likely (rigorous conditions apply) have one (or several) curve(s) of solutions. Try this to get a neat overview of the function:

f = @(a,x,y) sin(a)/cos(a) - (2*sin(x) + sin(y))./(2*cos(x) + cos(y));
[x,y] = meshgrid(-pi:pi/1000:pi);

HTH
``` 0  Bjorn
12/22/2016 10:42:04 AM
```"Bjorn Gustavsson" wrote in message <o3gals\$nnv\$1@newscl01ah.mathworks.com>...

> Since you've got one equation and two variables you'll most likely (rigorous conditions apply) have one (or several) curve(s) of solutions. Try this to get a neat overview of the function:
>
> f = @(a,x,y) sin(a)/cos(a) - (2*sin(x) + sin(y))./(2*cos(x) + cos(y));
> [x,y] = meshgrid(-pi:pi/1000:pi);
> pcolor(x,y,f(pi/4,x,y)),shading flat,colorbar,hold on,contour(x,y,f(pi/4,x,y),[0 0],'k'),caxis([-10 10])
>
> HTH 0  Yang