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

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 Comments

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
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)

Sign in to comment.

 Accepted Answer

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

More Answers (2)

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 Comments

It does give output. Check the value of variable X after you run the code.
I don't have the function solutionsproblem. What is the output when you run
X
after running the code.
i want output in the form of for G=600 print the values of x(1) tox(7).
similarly all values of G from 600 to 1200 all values from x(1) to x(7). here for the above code only one value is comming from x(1) tox(7).
Can you attach the function solutionsproblem? It will make it easy to suggest a solution.
last
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
299.9926 299.9944 299.9961 299.9979 299.9997 300.0015 300.0032
>>
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.

Sign in to comment.

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 Comments

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;;
end
Tg1 = X(:,1);
Tg2 = X(:,2);
Ta = X(:,3);
Tl = X(:,4);
Tf = X(:,5);
Tb = X(:,6);
Th = X(:,7);
but after compiling both the answers remain same after changing this code also
syms G;
tic
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;
disp(X);
end
toc
Tg1 = X(:,1);
Tg2 = X(:,2);
Ta = X(:,3);
Tl = X(:,4);
Tf = X(:,5);
Tb = X(:,6);
Th = X(:,7);
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) - 1.9*10^(-8)*x(1)^(4) + 462.56 - 5.39*x(1) + 2.52*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
The output remains the same after changing the codes also what should be done kindly please help me out in solving this problem
as april last is my final submission date for my project
I do not understand what you are looking for. Perhaps this will help:
syms G;
tic
G = 600:100:1200;
x0 = [300;300;300;300;300;300;300];
options = optimset('Display','off');
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;
end
toc
Tg1 = X(:,1);
Tg2 = X(:,2);
Ta = X(:,3);
Tl = X(:,4);
Tf = X(:,5);
Tb = X(:,6);
Th = X(:,7);
results = table(Tg1, Tg2, Ta, Tl, Tf, Tb, Th);
disp(results)
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) - 1.9*10^(-8)*x(1)^(4) + 462.56 - 5.39*x(1) + 2.52*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
actually i want the values of x(1) to x(7) from G=600 to 700 when G=600 Tg1 = 300;
Tg2 = 301.5;
Ta = 305.8;
Tl = 302;
Tf = 302;
Tb = 302;
Th = 300; these values as initial values we get the values from x(1) to x(7)
AT G=700
Tg1 = will become x(1);
Tg2 = x(2);
Ta = x(3);
Tl = x(4);
Tf = x(5);
Tb = x(6);
Th = x(7);the values we got from the above iteration finally i want all values from x(1) to x(7)
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!