Solving system of 3 non-linear equation with dynamic equality output
1 次查看(过去 30 天)
显示 更早的评论
I need to solve the set of equations given below for x1,x2,x3 such that x1+x2+x3==420 (for t=0 to t=0.2), x1+x2+x3==500 (for t=0.2 to t=0.4) and x1+x2+x3==420 (after t=0.4).
I tried to use step function in the code as x1+x2+x3=420*(stepfun(0:0.1:5,1)). But it did not work. Please suggest possible change in the code to incorporate the above change.
syms x1 x2 x3 t
eqns = [
x1 == t*(10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^2*sign((18*x2)/125 - (24*x1)/125 + 111/50) + 10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^(1/2)*sign((18*x2)/125 - (24*x1)/125 + 111/50)) - t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) + 140
x2 == 2*t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) - t*(10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^2*sign((18*x2)/125 - (21*x3)/100 + 91/100) + 10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^(1/2)*sign((18*x2)/125 - (21*x3)/100 + 91/100)) - t*(10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^2*sign((18*x2)/125 - (24*x1)/125 + 111/50) + 10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^(1/2)*sign((18*x2)/125 - (24*x1)/125 + 111/50)) + 140
x3 == t*(10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^2*sign((18*x2)/125 - (21*x3)/100 + 91/100) + 10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^(1/2)*sign((18*x2)/125 - (21*x3)/100 + 91/100)) - t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) + 140
x1+x2+x3==420]
E2 = lhs(eqns)-rhs(eqns)
F = matlabFunction(E2, 'vars', {[x1,x2,x3], t})
T = linspace(0,50,500);
nT = length(T);
sols = zeros(nT, 3);
x0 = [140, 140, 140];
options = optimoptions(@fsolve, 'Algorithm', 'levenberg-marquardt', 'display', 'none');
have_warned = false;
for tidx = 1 : nT
[thissol, ~, exitflag] = fsolve(@(x) F(x,T(tidx)), x0, options);
sols(tidx, :) = thissol;
x0 = thissol;
if exitflag <= 0 & ~have_warned
warning('solution failure code %d starting at time = %g', exitflag, T(tidx));
have_warned = true;
end
end
plot(T, sols);
legend({'x1', 'x2', 'x3'});
0 个评论
采纳的回答
Torsten
2022-6-29
syms x1 x2 x3 t A
eqns = [
x1 == t*(10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^2*sign((18*x2)/125 - (24*x1)/125 + 111/50) + 10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^(1/2)*sign((18*x2)/125 - (24*x1)/125 + 111/50)) - t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) + 140
x2 == 2*t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) - t*(10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^2*sign((18*x2)/125 - (21*x3)/100 + 91/100) + 10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^(1/2)*sign((18*x2)/125 - (21*x3)/100 + 91/100)) - t*(10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^2*sign((18*x2)/125 - (24*x1)/125 + 111/50) + 10*abs((18*x2)/125 - (24*x1)/125 + 111/50)^(1/2)*sign((18*x2)/125 - (24*x1)/125 + 111/50)) + 140
x3 == t*(10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^2*sign((18*x2)/125 - (21*x3)/100 + 91/100) + 10*abs((18*x2)/125 - (21*x3)/100 + 91/100)^(1/2)*sign((18*x2)/125 - (21*x3)/100 + 91/100)) - t*(10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^2*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100) + 10*abs((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)^(1/2)*sign((24*x1)/125 - (36*x2)/125 + (21*x3)/100 - 313/100)) + 140
x1+x2+x3==A];
E2 = lhs(eqns)-rhs(eqns);
F = matlabFunction(E2, 'vars', {[x1,x2,x3], t, A})
T = linspace(0,50,500);
nT = length(T);
sols = zeros(nT, 3);
x0 = [140, 140, 140];
options = optimoptions(@fsolve, 'Algorithm', 'levenberg-marquardt', 'display', 'none');
have_warned = false;
for tidx = 1 : nT
A = (T(tidx)<=0.2)*420 + (T(tidx)>0.2 && T(tidx)<=0.4)*500 + (T(tidx)>0.4)*420;
[thissol, ~, exitflag] = fsolve(@(x) F(x,T(tidx),A), x0, options);
exitflag
sols(tidx, :) = thissol;
x0 = thissol;
if exitflag <= 0 & ~have_warned
warning('solution failure code %d starting at time = %g', exitflag, T(tidx));
have_warned = true;
end
end
plot(T, sols);
legend({'x1', 'x2', 'x3'});
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Nonlinear Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!