필터 지우기
필터 지우기

solving multiple equations with variable later

조회 수: 2 (최근 30일)
Yokuna
Yokuna 2021년 9월 23일
댓글: Walter Roberson 2021년 9월 25일
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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 9월 25일
syms x1 x2 xL1 xL2 t x3 x4
x1 = 140 + xL1;
x2 = 160 + xL2;
eqn1 = x1 + x2 + x3 + x4 == 300 + xL1 + xL2
eqn1 = 
lhs(eqn1) - rhs(eqn1) == 0
ans = 
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
Walter Roberson
Walter Roberson 2021년 9월 25일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by