i need to solve 7 nonlinear equations with 7 unknowns and error is occurring while compiling the codes

2 次查看(过去 30 天)
G = 600:100:1200;
x0 = [300;300;300;300;300;300;300];
options = optimset('Display','off');
X(i) = NaN(1,length(G));
for i = 600:length(G)
fsol = fsolve(@(x) solutionsproblem(x,G(i)),x0,options);
X(i) = fsol(1);
end
function F = solutionsproblem(x,G)
Tg1 = 300;
Tg2 = 301.5;
Ta = 305.8;
Tl = 302;
Tf = 302;
Tb = 302;
Th = 300;
F(1) = 0.0425*G + 1.41*10^(-8)*x(2)^(4) + 1.34*x(2) - 9.12*10^(-9)*x(1)^(4) + 341 - 2402.77*x(1) + 2400*Tg1;
F(2) = 0.0204*G - (3.48*10^(-8) + 4.35*10^(-10)*1)*x(2)^(4) + 1.41*10^(-8)*x(1)^(4) + 8.01*10^(-11)* x(4)^(4) + 2.43*10^(-8)*x(3)^(4) + 4.35*10^(-10)*1*x(5)^(4) + 0.55*x(3) + 1.34*x(1) - 2401.87*x(2) + 2400*Tg2;
F(3) = 0.124*G - 4.88*10^(-8)*x(3)^(4) + 2.44*10^(-8)*1*x(2)^(4) + 2.44*10^(-8)*1*x(5)^(4)- 50.938*x(3) + 11.502*x(6) + 1.836* x(2) + 37.60*Ta;
F(4) = 0.011*G - 8.70*10^(-8)*x(5)^(4) + 4.35*10^(-10)*x(3)^(4)+ 4.35*10^(-10)*x(2)^(4) + 0.83*x(6) + 0.112*x(3) + 0.112*x(2) - (1.055 + (112.7/1))*x(5) + (112.7/1)*Tf;
F(5) = 2.17*10^(-3)*G - 1.6*10^(-10)*x(4)^(4) + 8.01*10^(-11)*x(7)^(4) + 8.01*10^(-11)*x(2)^(4) - 69.07*x(4) + 3.77*10^(-4)*x(7) + 0.02*x(3) + 0.02*x(2) + 69*Tl;
F(6) = -52.068*x(6) + 0.412*x(3) + 0.368*x(4)+ 0.368*x(5) + 0.46*x(2) + 50.78*Tb;
F(7)= 3.77*10^(-4)*x(4) + 8.01*10^(-11)*x(4)^(4) + 0.092*x(5) - 1254.1*x(7) - 8.01*10^(-11)*x(7)^(4) + 1254*Th;
end
  3 个评论
Bindhu PR
Bindhu PR 2020-4-8
Array indices must be positive integers or logical values.
Error in final (line 4)
X(i) = NaN(1,length(G));
THIS IS SHOWN IN COMMAND WINDOW
David Goodmanson
David Goodmanson 2020-4-8
Hi Bindhu,
you have not defined the argument of X. Ordinarily if you had, say, X(b) it would complain about the undefined variable b. Here, unless you have already defined i as something else, it is assuming i = sqrt(-1)

请先登录,再进行评论。

采纳的回答

Bindhu PR
Bindhu PR 2020-4-12
编辑:Ameer Hamza 2020-4-12
syms G;
G = 600:100:1200;
x0 = [300;300;300;300;300;300;300];
options = optimset('Display','on');
X = zeros(1,length(G)); % use X, instead of X(i), initialize with zeros
for i = 1:length(G) % start the loop counter from 1
fsol = fsolve(@(x) solutionsproblem(x,G(i)),x0,options);
X(i) = fsol(1);
end
disp(X);
function F = solutionsproblem(x,G)
Tg1 = 300;
Tg2 = 301.5;
Ta = 305.8;
Tl = 302;
Tf = 302;
Tb = 302;
Th = 300;
F(1) = 0.0425*G + 1.41*10^(-8)*x(2)^(4) + 1.34*x(2) - 9.12*10^(-9)*x(1)^(4) + 341 - 2402.77*x(1) + 2400*Tg1;
F(2) = 0.0204*G - (3.48*10^(-8) + 4.35*10^(-10)*1)*x(2)^(4) + 1.41*10^(-8)*x(1)^(4) + 8.01*10^(-11)* x(4)^(4) + 2.43*10^(-8)*x(3)^(4) + 4.35*10^(-10)*1*x(5)^(4) + 0.55*x(3) + 1.34*x(1) - 2401.87*x(2) + 2400*Tg2;
F(3) = 0.124*G - 4.88*10^(-8)*x(3)^(4) + 2.44*10^(-8)*1*x(2)^(4) + 2.44*10^(-8)*1*x(5)^(4)- 50.938*x(3) + 11.502*x(6) + 1.836* x(2) + 37.60*Ta;
F(4) = 0.011*G - 8.70*10^(-8)*x(5)^(4) + 4.35*10^(-10)*x(3)^(4)+ 4.35*10^(-10)*x(2)^(4) + 0.83*x(6) + 0.112*x(3) + 0.112*x(2) - (1.055 + (112.7/1))*x(5) + (112.7/1)*Tf;
F(5) = 2.17*10^(-3)*G - 1.6*10^(-10)*x(4)^(4) + 8.01*10^(-11)*x(7)^(4) + 8.01*10^(-11)*x(2)^(4) - 69.07*x(4) + 3.77*10^(-4)*x(7) + 0.02*x(3) + 0.02*x(2) + 69*Tl;
F(6) = -52.068*x(6) + 0.412*x(3) + 0.368*x(4)+ 0.368*x(5) + 0.46*x(2) + 50.78*Tb;
F(7)= 3.77*10^(-4)*x(4) + 8.01*10^(-11)*x(4)^(4) + 0.092*x(5) - 1254.1*x(7) - 8.01*10^(-11)*x(7)^(4) + 1254*Th;
end
  3 个评论

请先登录,再进行评论。

更多回答(2 个)

Ameer Hamza
Ameer Hamza 2020-4-8
编辑:Ameer Hamza 2020-4-12
It is a common practice to initialize arrays with zeros instead of nan. Correct two lines in your code
syms G;
G = 600:100:1200;
x0 = [300;300;300;300;300;300;300];
options = optimset('Display','off');
X = zeros(numel(G), numel(x0)); % use X, instead of X(i), initialize with zeros
for i = 1:length(G) % start the loop counter from 1
X(i,:) = fsolve(@(x) solutionsproblem(x,G(i)),x0,options);
end
disp(X);
function F = solutionsproblem(x,G)
Tg1 = 300;
Tg2 = 301.5;
Ta = 305.8;
Tl = 302;
Tf = 302;
Tb = 302;
Th = 300;
F(1) = 0.0425*G + 1.41*10^(-8)*x(2)^(4) + 1.34*x(2) - 9.12*10^(-9)*x(1)^(4) + 341 - 2402.77*x(1) + 2400*Tg1;
F(2) = 0.0204*G - (3.48*10^(-8) + 4.35*10^(-10)*1)*x(2)^(4) + 1.41*10^(-8)*x(1)^(4) + 8.01*10^(-11)* x(4)^(4) + 2.43*10^(-8)*x(3)^(4) + 4.35*10^(-10)*1*x(5)^(4) + 0.55*x(3) + 1.34*x(1) - 2401.87*x(2) + 2400*Tg2;
F(3) = 0.124*G - 4.88*10^(-8)*x(3)^(4) + 2.44*10^(-8)*1*x(2)^(4) + 2.44*10^(-8)*1*x(5)^(4)- 50.938*x(3) + 11.502*x(6) + 1.836* x(2) + 37.60*Ta;
F(4) = 0.011*G - 8.70*10^(-8)*x(5)^(4) + 4.35*10^(-10)*x(3)^(4)+ 4.35*10^(-10)*x(2)^(4) + 0.83*x(6) + 0.112*x(3) + 0.112*x(2) - (1.055 + (112.7/1))*x(5) + (112.7/1)*Tf;
F(5) = 2.17*10^(-3)*G - 1.6*10^(-10)*x(4)^(4) + 8.01*10^(-11)*x(7)^(4) + 8.01*10^(-11)*x(2)^(4) - 69.07*x(4) + 3.77*10^(-4)*x(7) + 0.02*x(3) + 0.02*x(2) + 69*Tl;
F(6) = -52.068*x(6) + 0.412*x(3) + 0.368*x(4)+ 0.368*x(5) + 0.46*x(2) + 50.78*Tb;
F(7)= 3.77*10^(-4)*x(4) + 8.01*10^(-11)*x(4)^(4) + 0.092*x(5) - 1254.1*x(7) - 8.01*10^(-11)*x(7)^(4) + 1254*Th;
end
  11 个评论
Bindhu PR
Bindhu PR 2020-4-12
This is the output I am getting .Can you help me to print output in such a way that for G=600 print values of x(1) to x(7) similaryy for G=600 to 1200.

请先登录,再进行评论。


Bindhu PR
Bindhu PR 2020-4-13
what modification in this programe is to be done so that for G=600 the values thus obtained is from x(1) tox(7) this values of x(1) to x(7) is given to G=700 and similarly the values for T are changed
Tg1 = x(1);
Tg2 = x(2);
Ta = x(3);
Tl = x(4);
Tf = x(5);
Tb = x(6);
Th = x(7);
  10 个评论
Walter Roberson
Walter Roberson 2020-4-28
If you want to print out the values of x(1) to x(7), why not just disp() the values? You have already shown that you know how to use disp(), so we do not understand why you are asking this question. It tends to suggest to us that you want something more complicated than just displaying x(1) to x(7).
I already showed you in https://www.mathworks.com/matlabcentral/answers/516059-i-need-to-solve-7-nonlinear-equations-with-7-unknowns-and-error-is-occurring-while-compiling-the-cod#comment_827486 how to store all of the values as you compute them, and put them into the various named variables; it would be trivial to add appropriate disp() to that. But you seemed to want something else other than what I suggested and did not reply to me, and you did not explain how what you wanted differed from what I suggested to you; it did not seem to be worth my time to keep asking you questions about what you really wanted.
You are getting 7 outputs for each of the 7 values of G. Give us an example of how you would like the output to look.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by