Using fminsearch to solve an equation - matlab

(vgb-phy_s)^2=G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))
where
x=phy_s/phy_t
phy_t=0.0288; % phy_t=k*T/q; (k=1.3806503*10^-23, T=300 K, q=1.6*10^-19)
phy_b=0.5267; % phy_b=phy_t*ln(Na/ni)
G=(sqrt(2*q*es*Na)/cox);
Here I need to plot phy_s for different values of vgb.
I tried many ways but since I'm new to matlab I'm on my learning process, I'm not able to find a proper solution.
Few people suggested me to use fminsearch but its quite confusing and I'm getting lot of errors.

fminsearch is a function for finding a minimum of a function, not for finding a solution of an equation. Further, here you don't have one equation but an equation group of at least 5 equations. You can use solve to solve equations and equation groups. However, an equation group of the following equations 1-5 does not have explicit solution. Another issue is that the constant values you propose seem to be imprecise values, and if you have more than one rounded or otherwise imprecise value, you can not find a solution even if the equation group was solvable (however, this equation group does not have [an explicit] solution)).
So, I'll show the steps to solve this but there seems to be something wrong with this equation group, even if the [probably imprecise] constant definitions (phy_t=0.0288; phy_t=k*T/q; (k=1.3806503*10^-23; T=300; q=1.6*10^-19; phy_b=0.5267;`) were left out.
Equations (without constant definitions):
1. (vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))
2. x = phy_s/phy_t
3. phy_t = k*T/q
4. phy_b=phy_t*ln(Na/ni)
5. G=(sqrt(2*q*es*Na)/cox)
To solve eg. equation group of equations 1, 2 & 3:
Solution = solve('(vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))', 'x = phy_s/phy_t', 'phy_t = k*T/q');
Solution.q
ans =
(T*k)/phy_t
(T*k)/phy_t
Solution.vgb
ans =
phy_s + (G*phy_t^(1/2)*(exp((2*phi_b)/phi_t) - exp(phy_s/phy_t) + exp((2*phy_s)/phy_t) - exp((2*phi_b)/phi_t)*exp(phy_s/phy_t) - (phy_s*exp(phy_s/phy_t))/phy_t + (phy_s*exp((2*phi_b)/phi_t)*exp(phy_s/phy_t))/phy_t)^(1/2))/(exp((2*phi_b)/phi_t)^(1/2)*exp(phy_s/phy_t)^(1/2))
phy_s - (G*phy_t^(1/2)*(exp((2*phi_b)/phi_t) - exp(phy_s/phy_t) + exp((2*phy_s)/phy_t) - exp((2*phi_b)/phi_t)*exp(phy_s/phy_t) - (phy_s*exp(phy_s/phy_t))/phy_t + (phy_s*exp((2*phi_b)/phi_t)*exp(phy_s/phy_t))/phy_t)^(1/2))/(exp((2*phi_b)/phi_t)^(1/2)*exp(phy_s/phy_t)^(1/2))
Solution.x
ans =
phy_s/phy_t
phy_s/phy_t
Do note that this solution is valid only for equation group of equations 1-3. For example equation group of equations 1, 2, 4 or 1, 2, 5 gives a different solution.
To solve the equation group of all 5 equations you could use this:
Solution = solve('(vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))', 'x = phy_s/phy_t', 'phy_t = k*T/q', 'phy_b = phy_t*ln(Na/ni)', 'G = sqrt(2*q*es*Na)/cox');
However, there is no solution:
Warning: Explicit solution could not be found.
In solve at 160
Solution =
[ empty sym ]
So, I suggest that you try to find out what is wrong with your equations and then try solve again with corrected equations.

Related

Can you please tell me how to solve four transcendental equations of four unknowns?

I have tried solving 4 equations of 4 unknowns in MATLAB and Mathematica.
I used vpasolve for finding the unknowns in MATLAB. Here is the MATLAB code.
Y1 = 0.02;
l1 = 0.0172;
syms Y2 Y3 l2 l3;
lambda = [0.0713 0.0688 0.0665];
b1 = 0.1170;
b3 = 0.1252;
t2_1 = (2*pi/lambda(1))*l2;
t3_1 = (2*pi/lambda(1))*l3;
t2_3 = (2*pi/lambda(3))*l2;
t3_3 = (2*pi/lambda(3))*l3;
t1_1 = (2*pi/lambda(1))*l1;
t1_3 = (2*pi/lambda(3))*l1;
eq1 = 2*Y1*tan(t1_1)+Y2*tan(t2_1)+Y3*tan(t3_1)==0;
eq2 = 2*Y1*tan(t1_3)+Y2*tan(t2_3)+Y3*tan(t3_3)==0;
eq3 = b1== (t1_1*Y1)+(t2_1*(Y2/2)*((sec(t2_1)^2)/(sec(t1_1)^2)))+(t3_1*(Y3/2)*((sec(t3_1)^2)/(sec(t1_1)^2)));
eq4 = b3== (t1_3*Y1)+(t2_3*(Y2/2)*((sec(t2_3)^2)/(sec(t1_3)^2)))+(t3_3*(Y3/2)*((sec(t3_3)^2)/(sec(t1_3)^2)));
E=[eq1 eq2 eq3 eq4];
S=vpasolve(E,[Y2,Y3,l2,l3]);
For the same equations, I wrote the below code in Mathematica.
eqns = {2*Y1*Tan[t11]+Y2*Tan[t21]+Y3*Tan[t31]==0,
2*Y1*Tan[t13]+Y2*Tan[t23]+Y3*Tan[t33]==0,
t11*Y1 + t21*(Y2/2)*(Sec[t21]^2/Sec[t11]^2) + t31*(Y3/2)*(Sec[t31]^2/Sec[t11]^2)-b1==0,
t13*Y1 + t23*(Y2/2)*(Sec[t23]^2/Sec[t13]^2) + t33*(Y3/2)*(Sec[t33]^2/Sec[t13]^2)-b3==0};
NMinimize[Norm[Map[First, eqns]], {Y2,Y3,l2,l3}]
But both are giving me different solutions and those are not the required solutions. I suppose I should use some other function for solving the equations. Can anyone help me finding out how to solve these equations? Your help is highly appreciated. Thank you.
EDIT
If I use the below code for finding the roots, I'm able to find the solutions but I just want positive roots. I tried the code which you have mentioned for getting positive roots but its not working for this and I don't know why. Can you please check this once?
Y1=0.0125;l1=0.010563;lambda={0.0426,0.0401,0.0403,0.0423,0.0413}; b1 = 0.0804;
b3 = 0.0258;
t2_1 = (2*Pi/lambda[[1]])*l2;
t3_1 = (2*Pi/lambda[[1]])*l3;
t2_3 = (2*Pi/lambda[[5]])*l2;
t3_3 = (2*Pi/lambda[[5]])*l3;
t1_1 = (2*Pi/lambda[[1]])*l1;
t1_3 = (2*Pi/lambda[[5]])*l1;
eqns = {2*Y1*Tan[t11]+Y2*Tan[t21]+Y3*Tan[t31]==0,
2*Y1*Tan[t13]+Y2*Tan[t23]+Y3*Tan[t33]==0,
t11*Y1 + t21*(Y2/2)*(Sec[t21]^2/Sec[t11]^2) + t31*(Y3/2)*(Sec[t31]^2/Sec[t11]^2)-b1==0,
t13*Y1 + t23*(Y2/2)*(Sec[t23]^2/Sec[t13]^2) + t33*(Y3/2)*(Sec[t33]^2/Sec[t13]^2)-b3==0};
Try
Y1=0.02;l1=0.0172;lambda={0.0713,0.0688,0.0665};b1=0.1170;b3=0.1252;
t21=(2*Pi/lambda[[1]])*l2;t31=(2*Pi/lambda[[1]])*l3;t23=(2*Pi/lambda[[3]])*l2;
t33=(2*Pi/lambda[[3]])*l3;t11=(2*Pi/lambda[[1]])*l1;t13=(2*Pi/lambda[[3]])*l1;
eqns={2*Y1*Tan[t11]+Y2*Tan[t21]+Y3*Tan[t31]==0,
2*Y1*Tan[t13]+Y2*Tan[t23]+Y3*Tan[t33]==0,
t11*Y1+t21*(Y2/2)*(Sec[t21]^2/Sec[t11]^2)+t31*(Y3/2)*(Sec[t31]^2/Sec[t11]^2)-b1==0,
t13*Y1+t23*(Y2/2)*(Sec[t23]^2/Sec[t13]^2)+t33*(Y3/2)*(Sec[t33]^2/Sec[t13]^2)-b3==0};
tbl=Table[
{y2i,y3i,l2i,l3i}=RandomReal[{0,.2},4];
Quiet[root=Check[FindRoot[eqns,{{Y2,y2i,0,.2},{Y3,y3i,0,.2},{l2,l2i,0,.2},{l3,l3i,0,.2}}],False]];
If[root===False,Nothing,root]
,{256}];
roots=Sort[Map[{Y2,Y3,l2,l3}/.#&,tbl],Norm[#1]<Norm[#2]&]
If[roots=={},"It found no roots in that range using those coefficients",
Map[First,eqns]/.{Y2->roots[[1,1]],Y3->roots[[1,2]],l2->roots[[1,3]],l3->roots[[1,4]]}]
That will look for roots starting at 256 different random locations greater than, but near 0. The way I have written FindRoot this time it will stop if the search is outside the range 0,.2 You can change that range if needed.
Next your functions have many local minima that trap FindRoot. So it should discard all local minima found inside that range.
And I have hidden the warning messages, usually not a good idea to do.
Then it will Sort the roots to show those nearest 0 first.
If you want it to sort to show the results nearest some given value then I can modify the Sort to show those first, I just need to know where you want the roots nearest to.
And finally it substitutes the Y2,Y3,l2,l3 of the smallest root it found back into the four equations to demonstrate that the results are very very near zero and this is a real root instead of a local minima.
After trying that a few times I found one root at {0.0203704,0.0225972,0.0163842,0.0181147} and that looks very close to your required values if I swap Y2 and Y3 and swap l2 and l3. Is that perhaps the root that you are looking for?
If you need more then please tell me exactly what is needed.
Please check ALL this VERY carefully to make certain I have made no mistakes.

matlab help in finding dimensions

Can anybody help me with this assignment please?
I am new to matlab, and passing this year depends on this assignment, i don't have much time to explore matlab and i already wasted alot of time trying to do this assignment in my way.
I have already wrote the equations on the paper, but transfering the equations into matlab codes is really hard for me.
All i have for now is:
syms h
l = (0.75-h.^2)/(3*sqrt((5*h.^2)/4)); %h is h_max
V_default = (h.^2/2)*l;
dv = diff(V_default); %it's max. when the derivative is max.
h1 = solve( dv ==0);
h_max = (h1>0);
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
but it keep give me error "Error using ./
Matrix dimensions must agree.
Error in triangle (line 9)
V_max = ((h_max.^2)./(2.*l_max)); "
Not really helping with the assignment here, but with the Matlab syntax. In the following line:
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
you're using / that is a matrix divide. You might want to use ./ which will divide the terms element by element. If I do this
l_max = (0.75-h_max.^2) ./ (3*sqrt((h_max/2).^2+(h_max.^2)));
then your code doesn't return any error. But I have no idea if it's the correct solution of your assignment, I'll leave that to you!
In line 5, the result h1 is a vector of two values but the variable itself remains symbolic, from the Symbolic Math Toolbox. MATLAB treats such variables slightly different. For that reason, the line h_max = (h1>0) doesn't really do what you expect. As I think from this point, you are interested in one value h_max, I would convert h1 to a regular MATLAB variable and change your code to the following:
h1 = double(solve( dv ==0)); % converts symbolic to regular vectors
h_max = h1(h1>0); % filters out all negative and zero values
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
EDIT.
If you still have error, it means solve( ...) returns more than 1 positive values. In this case, as suggested, use dotted operations, such as ./ but the results in l_max and V_max will not be a single value but vectors of the same size as h_max. Which means you don't have one max Volume.

Matlab's solve: Solution not satisfying the equation

I am trying to solve equations with this code:
a = [-0.0008333 -0.025 -0.6667 -20];
length_OnePart = 7.3248;
xi = -6.4446;
yi = -16.5187;
syms x y
[sol_x,sol_y] = solve(y == poly2sym(a), ((x-xi)^2+(y-yi)^2) == length_OnePart^2,x,y,'Real',true);
sol_x = sym2poly(sol_x);
sol_y = sym2poly(sol_y);
The sets of solution it is giving are (-23.9067,-8.7301) and (11.0333,-24.2209), which are not even satisfying the equation of circle. How can I rectify this problem?
If you're trying to solve for the intersection of the cubic and the circle, i.e., where y==poly2sym(a) equals (x-xi)^2+(y-yi)^2==length_OnePart^2 it looks like solve may be confused about something when the circle is represented parametrically rather than as single valued functions. It might also have to do with the fact that x and y are not independent solutions, but rather that the latter depends on the former. It also could depend on the use of a numeric solver in this case. solve seems to work fine with similar inputs to yours, so you might report this behavior to the MathWorks to see what they think.
In any case, here is a better, more efficient way to to tackle this as a root-solving problem (as opposed to simultaneous equations):
a = [-0.0008333 -0.025 -0.6667 -20];
length_OnePart = 7.3248;
xi = -6.4446;
yi = -16.5187;
syms x real
f(x) = poly2sym(a);
sol_x = solve((x-xi)^2+(f(x)-yi)^2==length_OnePart^2,x)
sol_y = f(sol_x)
which returns:
sol_x =
0.00002145831413371390464567553686047
-13.182825373861454619370838716408
sol_y =
-20.000014306269544436430325843024
-13.646590348358951818881695033728
Note that you might get slightly more accurate results (one solution is clearly at 0,-20) if you represent your coefficients and parameters more precisely then just four decimal places, e.g., a = [-1/1200 -0.025 -2/3 -20]. In fact, solve might be able to find one or more solutions exactly, if you provide exact representations.
Also, in your code, the calls to sym2poly are doing nothing other than converting back to floating-point (double can be used for this) as the inputs are not in the form of symbolic polynomial equations.

Solve trigonometrics equations in Matlab

I'm trying to solve for t trigonometric equations in Matlab, as i.e. 7*cos(t) + 5*sin(t) = 0. I would solve it as: sin(t)/cos(t) = -7/5 and I would find it as arctan(-7/5) = -0.9505.
I have tried to do it on matlab using solve function:
syms t
theta = solve(7*cos(t) + 5*sin(t)==0, t);
disp(theta);
But I get -(log(- 12/37 - (35*i)/37)*i)/2 instead of -0.9505. Could someone explain me why I get this answer from solve and how to get -0.9505?
The expression is the exact result, expressed symbolically (due to the use of syms).
To make Matlab display the result in the format your looking for use double(theta)
which should give you:
double(theta)
ans =
-0.9505

How can I solve 2 complex equations with 2 variables in MATLAB?

I have these two equations:
y1=a*(10/11- (3*i)/4) + b*(5/6+ (7*i)/5)
y2= -1+(j*2)
where: y1=y2 , And I want to find the exact value of "a" and "b" using only MATLAB.
Is there any MATLAB command I should use to solve these two equations??
p.s.: I tried to use solve command, but it doesn't give me any answer:
syms a b
y1=a*(10/11- (3*i)/4) + b*(5/6+ (7*i)/5);
y2= -1+(j*2);
s=solve('y1-y2=0',[a b])
It gives me this:
Warning: Explicit solution could not be found.
> In solve at 160
s =
[ empty sym ]
First, make sure you wrote your equations properly (operation precedence, parentheses):
in y1, the second and third terms are written weird:
if you simplify (according to what you wrote) it just becomes (45/124)*i + b*(67/30)
Also, why mix i and j in y2 ?
If you did all this well, and you still get the same answer, it really means there is no solution possible.
EDIT:
And looking at this again, you don't have a 2 equation / 2 variable system, you have 3 variables (y,a,b)... which means you can't solve.
EDIT 2:
From the last comment: well just do what you say you want to do, equalize the real and imaginary part of both equations:
syms a;
S = solve('a*(10/11)+b*(5/6)=-1','a*(3/4)+b*(7/5)=2');
S = [S.a S.b]
S =
[-4048/855, 226/57]
>> syms a b
>> solve( a*(10/11- (3*i)/4) + (3/4*i+ ((12)/(31*i))) + b*(5/6+ (7*i)/5i)==-1+(j*2))
a*(- 300/737 + (45*i)/134) - 30/67 + (3045*i)/4154
See official doc.