"Solve" command with a vector in matlab - matlab

I have a problem with the equation shown below. I want to enter a vector in t2 and find the roots of the equation from different values in t2.
t2=[10:10:100]
syms x
p = x^3 + 3*x - t2;
R = solve(p,x)
R1 = vpa(R)

Easy! Don't use syms and use the general formula:
t2 = [10:10:100];
%p = x^3 + 3*x - t2;
a = 1;
b = 0;
c = 3;
d = -t2;
D0 = b*b - 3*a*c;
D1 = 2*b^3 - 9*a*b*c + 27*a^2*d;
C = ((D1+sqrt(D1.^2 - 4*D0.^3))/2).^(1/3);
C1 = C*1;
C2 = C*(-1-sqrt(3)*1i)/2;
C3 = C*(-1+sqrt(3)*1i)/2;
f = -1/(3*a);
x1 = f*(b + C1 + D0./C1);
x2 = f*(b + C2 + D0./C2);
x3 = f*(b + C3 + D0./C3);
Since b = 0, you can simplify this a bit:
% ... polynomial is the same
D0 = -3*a*c;
D1 = 27*a^2*d;
% ... the different C's are the same
f = -1/(3*a);
x1 = f*(C1 + D0./C1);
x2 = f*(C2 + D0./C2);
x3 = f*(C3 + D0./C3);

Trial>> syms x p
Trial>> EQUS = p == x^3 + 3*x - t2
It is unknown that you want to solve an equation or a system.
Suppose that you want to solve a system.
Trial>> solx = solve(Eqns,x)
But, I do not think you can find roots.
You can solve one equation.
Trial>> solx = solve(Eqns(3),x)
As far as I know, Maple can do this batter.

In general, loops should be avoided, but this is the only solution that hit my brain right now.
t2=[10:10:100];
pp=repmat([1,0,3,0],[10,1]);
pp(:,4)=-t2;
for i=1:10
R(i,:) =roots(pp(i,:))';
end

Related

How to fix Matlab Solve Calculation Error/solver gives a variable back instead of a numerical answer?

I'm trying to solve for q. However, this error, in the image attached, shows up and I have no clue how to fix it. The code runs smoothly without the fifth blob [k5*z5/(1+q*(k5-1))] in the equation and it solves for q. But when I add it to the equation, you get the error... Help....
z1 = 0.01188354;
z2 = 0.20291147;
z3 = 0.03386809;
z4 = 0.6087344;
z5 = 0.1426025;
k1 = 0.00211577;
k2 = 433.816504;
k3 = 0.00651267;
k4 = 12.8652437;
k5 = 3.25E-06;
syms q
eqn = k1*z1/(1+q*(k1-1)) + k2*z2/(1+q*(k2-1)) + k3*z3/(1+q*(k3-1)) + k4*z4/(1+q*(k4-1))+ k5*z5/(1+q*(k5-1)) == 1;
qvalue = solve(eqn,q,'Real',true)
You messed a bit with solve. It looks like your equation doesn't have real solution and you force it with 'Real',true It should be:
z1 = 0.01188354;
z2 = 0.20291147;
z3 = 0.03386809;
z4 = 0.6087344;
z5 = 0.1426025;
k1 = 0.00211577;
k2 = 433.816504;
k3 = 0.00651267;
k4 = 12.8652437;
k5 = 3.25E-06;
syms q
eqn = k1*z1/(1+q*(k1-1)) + k2*z2/(1+q*(k2-1)) + k3*z3/(1+q*(k3-1)) + k4*z4/(1+q*(k4-1))+ k5*z5/(1+q*(k5-1)) == 1;
qvalue = solve(eqn,q)
And the output is:
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 1)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 2)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 3)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)/146344515065711958525050250689493211538989295698771861545387136571113311336350613504 + (877647369043978904163970290106419962713606423192038983322127636310825879207936*z^3)/164487327751721471763565140886077743965784030575734255347857322244873703346919 - (3796080934806054258394941458065140559250786119491250988496078448019381612969984*z^2)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + (838076006172751803233223399262272123114675605549658858976242221943722830987264*z)/1151411294262050302344955986202544207760488214030139787435001255714115923428433 + 21452770321210561747759866301918995558818778263453224281332504760441104236544/1151411294262050302344955986202544207760488214030139787435001255714115923428433, z, 4)
root(z^5 - (553951988707451897271725042874967932463648393519489496501295701298760653356885564069*z^4)

Why is MATLAB returning different results for the same linear system?

I have this 4x4 system
where the variables are Eor, Eot, Eo2r and Eo2t (i.e., Eo, d, n1, n2, n3, g2 and g3 are known complex numbers).
I'm interested in finding not the variables themselves but these other values:
p_tot = Eor/Eo;
t_tot = Eot/Eo;
p_1 = Eo2r/Eo;
t_1 = Eo2t/Eo;
I tried to solve this using MATLAB, as doing it by hand would be rather tedious.
The values of the constants are these:
e0 = 8.854187817*10^(-12);
u0 = 4*pi*10^(-7);
n0 = sqrt(u0/e0);
f = 10^9;
w = 2*pi*f;
d = 0.3;
tg_p = 0.23;
g2 = w*sqrt(u0*e0*9)*sqrt(1-i*tg_p);
g3 = w*sqrt(u0*e0);
n1 = n0;
n2 = w*u0/g2;
n3 = n0;
Eo = 1;
When I solve the linear system with this script
syms Eor Eo2t Eo2r Eot
eq1 = Eo+Eor == Eo2t + Eo2r;
eq2 = Eo-Eor == (n1/n2)*(Eo2t-Eo2r);
eq3 = Eo2t*exp(-i*g2*d)+Eo2r*exp(i*g2*d) == Eot*exp(-i*g3*d);
eq4 = Eo2t*exp(-i*g2*d)-Eo2r*exp(i*g2*d) == (n2/n3)*Eot*exp(-i*g3*d);
[SEor, SEo2t, SEo2r, SEot] = solve([eq1,eq2,eq3,eq4] , [Eor, Eo2t, Eo2r, Eot]);
S = [SEor; SEo2t; SEo2r; SEot];
p_tot = double(SEor/Eo)
t_1 = double(SEo2t/Eo)
p_1 = double(SEo2r/Eo)
t_tot = double(SEot/Eo)
I get a completely different result from the one I get if I solve the system as Ax=b and then perform the MATLAB operation A\b:
A = [1 -1 -1 0;
1 n1/n2 -n1/n2 0;
0 exp(-j*g2*d) exp(j*g2*d) -exp(-j*g3*d);
0 exp(-j*g2*d) -exp(j*g2*d) -exp(-j*g3*d)*(n3/n2)];
b = [-1;1;0;0];
S = A\b;
p_tot = S(1)/Eo
t_1 = S(2)/Eo
p_1 = S(3)/Eo
t_tot = S(4)/Eo
So the thing is that I'm getting two different results for the same problem using two different methods (which should be equivalent).
The results I'm getting are these:
With the Ax = b method:
p_tot =
-0.5109 + 0.0436i
t_1 =
0.4924 + 0.0427i
p_1 =
-0.0032 + 0.0009i
t_tot =
0.0284 + 0.0011i
Directly solving the system:
p_tot =
-0.5011 + 0.0406i
t_1 =
0.4956 + 0.0415i
p_1 =
0.0033 - 0.0009i
t_tot =
0.0866 - 0.0066i
What am I doing wrong and which one is the actual solution?
The problem is that in your A matrix, the value in the 4th row, 4th column doesn't reflect the system of equations you presented in your question.
Instead of (n3/n2) it should be (n2/n3).

Matlab: Estimating coefficients of nonlinear differential equations

Need to solve the system of nonlinear differential equations:
x1p = a1*u2*x1^1.3 + a2*u1 + a3*u3
x2p = (a4*u2 + a5)*x1^1.3 + a6*x2
x3p = (a7*u3 + (a8*u2-a9)*x1)/a10
x1p, x2p & x3p are time derivatives of x1, x2 & x3, i.e. dx1/dt, dx2/dt & dx3/dt.
we have descrete data of x1, x2 & x3 as well as of u1, u2 & u3. We need to solve the problem in order to get the unknown coefficients, a1, a2, …, a10.
Have checked many posts and can say the solution involves ODE45 (or other ODEX) and probably fsolve or fminsearch (Matlab), but have not managed to setup up the problem correctly, guess we don't understand the coding well. Please, suggestions.
you should replace x1p, x2p ,and x3p by using definition of derivative:
x1p = (x1(i+1) - x(i))/ dt , and like this for the others.
then use folowing algorithm (it is not complete):
descrete data of x1, x2 & x3 as well as of u1, u2 & u3
dt = 0.01
myFun = #(a,x1,x2,x3,u1,u2,u3)
[ (x1(i+1) - x1(i))/ dt = a(1)*u2(i)*x1(i)^1.3 + a(2)*u1(i) + a(3)*u3(i);
(x2(i+1) - x2(i))/ dt = (a(4)*u2(i) + a(5)*x1(i)^1.3 + a(6)*x2(i);
(x3(i+1) - x3(i))/ dt = (a(7)*u3(i) + (a(8)*u2(i)-a(9))*x1(i))/a(10) ]
A=[];
a0 = [0; 0; 0 ;0 ;.... ]
for i= 1:1: lenngth(x1)
a=fsolve(#(a)myFun(a,x1,x2,x3,u1,u2,u3),a0,options);
a0 = [ a(1,1) ; a(2,1); a(3,1) ; .......]
A = cat(1,A,a) ;
end
a1 = mean(A(1,:))
a2 = mean(A(2,:))
.
.
a10 = mean(A(10,:))

Plotting parameterized solutions in matlab

I need to plot parameterized solutions for the following systems of equations using t values from 0 to 0.3 incremented by 0.001 each time:
x′  =  12.3 x  −  2.7 y
y′  =  5.7 x  −  3.7 y
This is what I have so far, but I'm pretty sure my parametric curves are wrong. I'd be expecting some exponential looking thing, not a lot of straight lines. What am I doing wrong?
A = [ 12.3, -2.7; 5.7, -3.7 ]; %initial matrix
[P D] = eig(A); %finding eigenvalues and eigenvectors
i = [1;4.3]; %initial conditions
H = inv(P)*i; %solving for c1 and c2
t = 0:0.001:0.3;
c1 = 0.2580; %constant
c2 = 4.2761; %constant
B1 = [0.9346;0.3558]; %eigenvector
B2 = [0.1775;0.9841]; %eigenvector
a = 11.2721; %eigenvalue
b = -2.6721; %eigenvalue
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
plot(x1,x2);
Your problem was calculating x1 and x2. Since B1 and B2 are vectors, doing this:
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
made x1 and x2 2 by 301 matrices.
The correct result is simpler:
x = c1*B1*exp(a*t) + c2*B2*exp(b*t);
and plotting it gives:
plot(x(1,:),x(2,:));

matlab: large system of coupled nonlinear equations

I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus