How can I solve 2 complex equations with 2 variables in MATLAB? - 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.

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.

Solve equation with exponential term

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

how can I solve a redundant symbolic system in matlab?

let
n0 =
nx*cos(a) + nz*cos(b)*sin(a) + ny*sin(a)*sin(b)
ny*cos(b) - nz*sin(b)
nz*cos(a)*cos(b) - nx*sin(a) + ny*cos(a)*sin(b)
in a and b,with the ns fixed (but of course,not assigned) values.
if I do
[a,b]=solve(n0-[1 0 0]',a,b,'IgnoreAnalyticConstraints',true)
i get
Error using solve>assignOutputs (line 257)
3 variables does not match 2 outputs.
Error in solve (line 193)
varargout = assignOutputs(nargout,sol,sym(vars));
then I wonder ''3 variables''?
Then I try
>> [a,b,c]=solve(n0-[1 0 0]',a,b,'IgnoreAnalyticConstraints',true)
that's the response
a =
cos(a)/(cos(a)^2 + sin(a)^2)
b =
(sin(a)*sin(b))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
c =
(cos(b)*sin(a))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
what is it doing? what's in c? I suppose he's solving with respect to nx ny nz,but why?every time I try to solve a problem with n+k equation in n variables I get strange errors,even if the rank of the system is just n.
that means even a=2 b=3 a+b=5 gives me problems.
how can I fix that?
I also cannot replicate the "Error in solve" error. What version of Matlab are you using? Also, I think some of the error message is missing – always list the entire error message. In any case, R2013a, solve does not find any solutions. Mathematica 9's Solve also does not find any.
I suspect why #DanielR and I can't exactly reduce your issue in the second case is that you may have a mistake in one of your lines above – it should be:
[a,b,c] = solve(n0-[1 0 0]','IgnoreAnalyticConstraints',true)
that produces
a =
cos(a)/(cos(a)^2 + sin(a)^2)
b =
(sin(a)*sin(b))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
c =
(cos(b)*sin(a))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
What are the outputs a, b, and c (these simplify to cos(a), sin(a)*sin(b), and sin(a)*cos(b), by the way)? A big hint is that all of the solutions are in terms of your original variables a and b, but not nx, ny, or nz. When you don't specify which variables to solve for solve picks them. If you instead return the solutions in structure form, the nature of the output is made clear:
s = solve(n0-[1 0 0]','IgnoreAnalyticConstraints',true)
s =
nx: [1x1 sym]
ny: [1x1 sym]
nz: [1x1 sym]
But I think that you probably want to solve for a and b as a function of nx, ny, and nz, not the other way around. You're not correct about using solve to find solutions to overdetermined systems. Even when you have more equations then unknowns this is not always possible with nonlinear equations. If you can introduce some assumptions or even additional equations or specify numerical values for any of the nx, ny, or nz variables, solve may be able to separate and invert the equations.
And you shouldn't really use the term "rank" except for linear systems. In the case of the linear system example that you gave solve works fine:
[a,b] = solve([a==2 b==3 a+b==5],a,b)
or
[a,b] = solve(a==2,b==3,a+b==5,a,b)
or
[a,b] = solve([1 0;0 1;1 1]*[a;b]==[2;3;5],a,b)
returns
Warning: 3 equations in 2 variables.
> In /Applications/MATLAB_R2013a.app/toolbox/symbolic/symbolic/symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
a =
2
b =
3

Matlab: linear congruence solver that supports a non-prime modulus?

I'm working on some Matlab code to perform something called the Index Calculus attack on a given cryptosystem (this involves calculating discrete log values), and I've gotten it all done except for one small thing. I cant figure out (in Matlab) how to solve a linear system of congruences mod p, where p is not prime. Also, this system has more than one variable, so, unless I'm missing something, the Chinese remainder theorem wont work.
I asked a question on the mathematics stackexchange with more detail/formatted mathjax here. I solved the issue in my question at that link, and now I'm attempting to find a utility that will allow me to solve the system of congruences modulo a non-prime. I did find a suite that includes a solver supporting modular arithmetic, but the modulus must be prime (here). I also tried stepping through to modify it to work with non-primes, but whatever method is used doesn't work, because it requires all elements of the system have inverses modulo p.
I've looked into using the ability in Matlab to call MuPAD functions, but from my testing, the MuPAD function linsolve (which seemed to be the best candidate) doesn't support non-prime modulus values either. Additionally, I've verified with Maple that this system is solvable modulo my integer of interest (8), so it can be done.
To be more specific, this is the exact command I'm trying to run in MuPAD:
linsolve([0*x + 5*y + 4*z + q = 2946321, x + 7*y + 2*q = 5851213, 8*x + y + 2*q = 2563617, 10*x + 5*y + z = 10670279],[x,y,z,q], Domain = Dom::IntegerMod(8))
Error: expecting 'Domain=R', where R is a domain of category 'Cat::Field' [linsolve]
The same command returns correct values if I change the domain to IntegerMod(23) and IntegerMod(59407), so I believe 8 is unsuitable because it's not prime. Here is the output when I try the above command with each 23 and 59407 as my domain:
[x = 1 mod 23, y = 1 mod 23, z = 12 mod 23, q = 14 mod 23]
[x = 14087 mod 59407, y = 1 mod 59407, z = 14365 mod 59407, q = 37320 mod 59407]
These answers are correct- x, y, z, and q correspond to L1, L2, L3, and L4 in the system of congruences located at my Math.StackExchange link above.
I'm wondering if you tried to use sym/linsolve and sym/solve previously, but may have passed in numeric rather than symbolic values. For example, this returns nonsense in terms of what you're looking for:
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(A,b),8)
But if you convert the numeric values to symbolic integers, sym/linsolve will keep everything in terms of rational fractions. Then
s = mod(linsolve(sym(A),sym(b)),8)
returns the expected answer
s =
6
1
6
4
This just solves the system linear system using symbolic math as if it were a normal matrix. For large systems this can be expensive, but I'd imagine no more than using MuPAD's numeric::linsolve or linalg::matlinsolve. sym/mod should return the modulus of the numerator of each solution component. I believe that you will get an error if the modulus and the denominator are not at least coprime.
sym/solve can also be used to solve this in a similar manner:
L = sym('L',[4,1]);
[L1,L2,L3,L4] = solve(A*L==b);
s = mod([L1;L2;L3;L4],8)
A possible issue with using either sym/solve or sym/linsolve is that if there are multiple solutions to the linear congruence problem (as opposed to the linear system), this approach may not return all of them.
Finally, using the MuPAD function numlib::ichrem (chinese remainder theorem for integers), here's some code that attempts to obtain the complete solution:
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
m = 10930888;
mf = str2num(strrep(char(factor(sym(m))),'*',' '));
A = sym(A);
b = sym(b);
s = sym(zeros(length(b),length(mf)));
for i = 1:length(mf)
s(:,i) = mod(linsolve(A,b),mf(i));
end
mstr = ['[' sprintf('%d,',mf)];
mstr(end) = ']';
r = sym(zeros(length(b),1));
for i = 1:length(b)
sstr = char(s(i,:));
r(i) = feval(symengine,'numlib::ichrem',sstr(9:end-2),mstr);
end
check = isequal(mod(A*r,m),b)
I'm not sure if any of this is what you're looking for, but hopefully it might be helpful. I think that it might be a good idea to put in a enhancement/service request with the MathWorks so that MuPAD and the other solvers can handle systems better in the future.

Using fminsearch to solve an equation

(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.