solving multiple equations with variable later
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I want to solve the set of equations and plot x1 vs t and x2 vs t. The problem is due to sols(tidx,1). I want to calculate x1 as 140 +sols(tidx,1)^2 as given in equations. The value of sols(tidx,1) is updating below in the loop. How to find x1 by updating xL1 at each instant?
syms x1 x2 xL1 xL2 t
eqns = [
x1-140-xL1==0;
x2-160-xL2==0;
xL1==sols(tidx,1)^2;
xL2==sols(tidx,2)^2;
x1+x2==300+xL1+xL2;];
E2 = lhs(eqns)-rhs(eqns);
F = matlabFunction(E2, 'vars', {[x1,x2,xL1,xL2], t});
T = linspace(0,20,500);
nT = length(T);
x0 = [140+140*140, 160+160*160, 140*140, 160*160];
sols = zeros(nT, 4);
options = optimoptions(@fsolve, 'Algorithm', 'levenberg-marquardt', 'display', 'none');
for tidx = 1 : nT
[thissol, ~, exitflag, output] = fsolve(@(x) F(x,T(tidx)), x0, options);
p=thissol;
sols(tidx,:)=p;
x0=[sols(tidx,1),sols(tidx,2),sols(tidx,3),sols(tidx,4)]
end
figure(1)
plot(T, sols(:,1));
hold on
plot(T, sols(:,2));
hold on
0 Commenti
Risposte (1)
Walter Roberson
il 25 Set 2021
syms x1 x2 xL1 xL2 t x3 x4
x1 = 140 + xL1;
x2 = 160 + xL2;
eqn1 = x1 + x2 + x3 + x4 == 300 + xL1 + xL2
lhs(eqn1) - rhs(eqn1) == 0
but we do not know what x3 and x4 are.
If x3 and x4 happen to satisfy that x3 == -x4 then your equations are always satisfied for finite xL1, xL2; otherwise they cannot be satisfied.
1 Commento
Walter Roberson
il 25 Set 2021
You edited your code. With the new code, the fifth equation is the sum of the first two equations, and x1 and x2 can be directly calculated from sols. There is no point going through fsolve.
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!