How to solve complex system of equations using matlab? - matlab

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?

I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0

Related

matlab solving numerical differential equations [pic]

I'm trying to solve this numerical differential equations, can someone help?
clc;
clear;
syms A1(z) A2(z)
lamda1 = 1560*(10^-9);
c=3*(10^8);
d_eff=27*(10^-12);
omga1=(2*pi*c)/(lamda1);
omga2=omga1*2;
n=2.2;
k1=(n*omga1)/c;
k2=(n*omga2)/c;
ode1 = diff(A1) == (2*i*(omga1^2)*d_eff*A2*conj(A1)*exp(-i*(2*k1-k2)*z))/(k1*(c^2));
ode2 = diff(A2) == (i*(omga2^2)*d_eff.*(A1.^2).*exp(i*(2*k1-k2)*z))/(k2*(c^2));
odes = [ode1; ode2];
cond1 = A1(0) == 1;
cond2 = A2(0) == 0;
conds = [cond1; cond2];
M = matlabFunction(odes)
sol = ode45(M(1),[0 20],[2 0]);
in this question both ODE are coupled, hence there's only 1 ODE to solve:
1.- use 1st equation to write
A1=f(A2,dA2/dz)
and feed this expression into 2nd equation.
2.- regroup
n1=1j*k1^4/k2^2*1/deff*.25
n2=1j*3*(k1-k2)
now the ODE to solve is
y'=n1/y^2*exp(n2*z)
3.- It can obviously be done in MATLAB, but for this particular ODE in my opinion the Wolfram online ODE Symbolic Solver does a better job.
Input the obtained ODE of previous point into the ODE solver available in this link
https://www.wolframalpha.com/input?i=y%27%27+%2B+y+%3D+0
and solve
4.- The general (symbolic) solutions for A2 are
Note that I used k1 instead of n1 and k2 instead of n2 just in the Wolfram ODE Solver.
Rewording ; the k1 k2 expressions of the general solutions are not the wave numbers k1 k2 of the equations in the question. Just replace accordingly.
5.- Now get A1 using expression in point 2.
6.- I have spotted 2 possible errors in the MATLAB code posted in the question that shouldn't be ignored by the question originator:
In the far right side of the posted MATLAB code, the exponential expressions
6.1.- both show
exp(-i(2*k1-k2)z))/(k1(c^2))
there's this (k1*(c^2)) dividing the exponent wheras in the question none of the exponentials show such denominator their respective exponents.
6.2.- the dk or delta k expression in the exponentials of the question are obviously k2-k1 or k1-k2 , here there may be room for a sign ambiguity, that may shoot a wave solution onto the opposite direction, yet the point here is where
*exp(-1i*(2*k1-k2)*z)
should probably be
*exp(-1i*2*(k1-k2)*z)
or just
exp(-1i*(k1-k2)*z)
6.3.- and yes, in MATLAB (-1)^.5 can be either expressed with 1j or 1i but as written in the MATLAB code made available in the question, since only a chunk of code has been made available, it's fair to assume that no such i=1j has been done.

Matlab: Reduce second order matrix differential equation to standard eigenproblem

I want to obtain the natural frequencies of a simple mechanical system with mass matrix M and stiffness matrix K (.mat-file -> Download):
Mx''(t)+Kx(t)=0 (x= Position).
It means basically, that I have to solve det(K-w^2*M)=0. But how can I solve it in Matlab (or if necessary reduce it to a standard eigenvalue problem and solve it then)? The matrices are definitely solvable with Abaqus (FEM Software), but I have to solve it in Matlab.
I tried the following without success: det(K-w^2*M)=0 => det(M^-1*K-w^2*I)=0 (I := unity matrix)
But solving this eigenvalue problem with
sqrt(eigs(K*M^-1))
delivers wrong values and the warning:
"Matrix is singular to working precision.
In matlab.internal.math.mpower.viaMtimes (line 35)"
Other wrong values can be obtained via det(K-w^2*M)=0 => det(I/(w^2)-M*K^-1)=0:
1./sqrt(eigs(M*K^-1))
Any hint would help me. Thanks in advance.
As #Arpi mentioned, you actually want to solve the generalized eigenvalue problem:
K*x = w^2*M*x
Since your matrices K and M are apparently singular (or just one of them), it is not possible to use eigs, but you have to use eig:
V = eig(K,M);
w = sqrt(V);

Using MATLAB's solve function to find solution to system of equations

I am having trouble solving a system of equations. I have three equations with a known solution and three unknowns in each equation. However, when I use the solve function in MATLAB, it returns with the error that I have six equations and three variables.
A snippet of my code:
syms V0 T0 X0
A=(g*X0/(2*V0^2*cos(T0)^2)-tan(T0))==a;
B=(tan(T0)-g*X0/(V0^2*cos(T0)^2))==b;
C=(-g/(2*V0^2*cos(T0)^2))==c;
soln=solve([A,B,C],[V0,T0,X0]);
I have already calculated scalar values for a, b, and c. g is a constant.
I am not sure why it is returning that I have six equations.
V0^2 means its a quadratic equation. You could solve for V0^2 as a variable. Set V0^2 = J0 and solve for J0 instead.
soln=solve([A,B,C],[J0,T0,X0]);
Then its three linear equations with three variables.
Once you get value of J0, then you need to solve for V0^2 = J0.

matlab differential equation

I have the following differential equation which I'm not able to solve.
We know the following about the equation:
D(r) is a third grade polynom
D'(1)=D'(2)=0
D(2)=2D(1)
u(1)=450
u'(2)=-K * (u(2)-Te)
Where K and Te are constants.
I want to approximate the problem using a matrix and I managed to solve
the similiar equation: with the same limit conditions for u(1) and u'(2).
On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:
clear
a=1;
b=2;
N=100;
h = (b-a)/N;
K=3.20;
Ti=450;
Te=20;
A = zeros(N+2);
A(1,1)=1;
A(end,end)=1/(2*h*K);
A(end,end-1)=1;
A(end,end-2)=-1/(2*h*K);
r=a+h:h:b;
%y(i)
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);
%y(i-1)
for i=1:1:length(r)-1
ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
end
A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);
%y(i+1)
for i=1:1:length(r)
ymax(i)=r(i)*(1/(h^2))+1/(2*h);
end
A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);
Y=zeros(N+2,1);
Y(1) =Ti;
Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
Y(end) = Te;
r=[1,r];
u=A\Y;
plot(r,u(1:end-1));
My question is, how do I solve the first differential equation?
As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be
D(r) = -2r^3+9r^2-12r+6
with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)
I would probably use this information right away, computing the coefficient k of the first derivative:
r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);
It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.
By the way, instead of loops like
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
one usually does simply
yi=-r*(2/(h^2));
This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

Curve Fitting for equation with two parameters

I have two arrays:
E= [6656400;
13322500;
19980900;
26625600;
33292900;
39942400;
46648900;
53290000]
and
J=[0.0000000021;
0.0000000047;
0.0000000128;
0.0000000201;
0.0000000659;
0.0000000748;
0.0000001143;
0.0000001397]
I want to find the appropriate curve fitting for the above data by applying this equation:
J=A0.*(298).^2.*exp(-(W-((((1.6e-19)^3)/(4*pi*2.3*8.854e-12))^0.5).*E.^0.5)./((1.38e-23).*298))
I want to select the starting value of W from 1e-19
I have tried the curve fitting tools but it is not helping me to solve it!
Then, I selected some random values of A0=1.2e9 and W=2.243e-19, it gave me a better results. But I want to find the right values by using the code (not the curve fitting Apps)
Can you help me please?
A quick (and potentially easy) solution method would be to pose the curve fit as a minimization problem.
Define a correlation function that takes the fit parameters as an argument:
% x(1) == A0; x(2) == W
Jfunc = #(x) x(1).*(298).^2.*exp(-(x(2)-((((1.6e-19)^3)/(4*pi*2.3*8.854e-12))^0.5).*E.^0.5)./((1.38e-23).*298));
Then a objective function to minimize. Since you have data J we'll minimize the sum-of-squares of the difference between the data and the correlation:
Objective = #(x) sum((Jfunc(x) - J).^2);
And then attempt to minimize the objective using fminsearch:
x0 = [1.2E9;2.243E-19];
sol = fminsearch(Objective,x0);
I used the guesses you gave. For nonlinear solutions, a good first guess is often important for convergence.
If you have the Optimization Toolbox, you can also try lsqcurvefit or lsqnonlin (fminsearch is vanilla MATLAB).