Solving 18 equations in iteration until 3 variables within a percent
1 次查看(过去 30 天)
显示 更早的评论
Example: following are the equations needed to be solved for convergence of A & B within 1% ; Initial assumption of A is 0.8, B is 0.375.
Eq 1: A = B/(1-C);
Eq 2: B = (D + E)/(F +G);
Eq 3: C = D*E + F*B;
Eq 4: D = 2*A/B; etc.
I could technically write my code using while loop as below, but the equations i have are actually complex and cannot be solved as i did.
I'm trying to find if there is a better way of doing such iterations with few or little known values and guesses.
P.S. The equations and values in here are just to demonstrate, it might or might not actually converge.
%known values
e = 10; f = 5; g = 1.75
% initial guess
a = 0.8;
b = 0.75;
% Temp variables
a_temp = a;
b_temp = b;
a_conv = 0;
b_conv = 0;
while i = 1:100 % to prevent from staying in loop forever
d = 2*a/b; % eq 4
b = (d + e)/(f + g); %eq 2
c = (d*e) + (f*g); % eq 3
a = b/(1-c); % eq 1
a_conv = ( a - a_conv)/a;
b_conv = (b - b_conv)/b;
if a_conv <0.01 && b_conv<0.01
break
else
a_conv = a;
b_conv = b;
end
end
3 个评论
J. Alex Lee
2020-9-18
have you read the docs for fsolve? you first need to set up the equations in residual form, i.e.,
r(1) = A - B/(1-C)
r(2) = B - (D + E)/(F +G);
r(3) = C - (D*E + F*B);
r(4) = D - 2*A/B;
etc.
Then you have a standard root finding problem, just plug into fsolve...
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!