i need to solve 7 nonlinear equations with 7 unknowns and error is occurring while compiling the codes
Afficher commentaires plus anciens
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 commentaires
darova
le 7 Avr 2020
Can you show the error?
Bindhu PR
le 8 Avr 2020
David Goodmanson
le 8 Avr 2020
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)
Réponse acceptée
Plus de réponses (2)
Ameer Hamza
le 8 Avr 2020
Modifié(e) : Ameer Hamza
le 12 Avr 2020
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 commentaires
Bindhu PR
le 11 Avr 2020
Bindhu PR
le 11 Avr 2020
Bindhu PR
le 11 Avr 2020
Ameer Hamza
le 11 Avr 2020
It does give output. Check the value of variable X after you run the code.
Bindhu PR
le 11 Avr 2020
Ameer Hamza
le 11 Avr 2020
I don't have the function solutionsproblem. What is the output when you run
X
after running the code.
Bindhu PR
le 11 Avr 2020
Ameer Hamza
le 11 Avr 2020
Can you attach the function solutionsproblem? It will make it easy to suggest a solution.
Bindhu PR
le 12 Avr 2020
Modifié(e) : Walter Roberson
le 15 Avr 2020
Bindhu PR
le 12 Avr 2020
Ameer Hamza
le 12 Avr 2020
Please check the updated code in the answer.
Bindhu PR
le 13 Avr 2020
0 votes
10 commentaires
Bindhu PR
le 14 Avr 2020
Walter Roberson
le 14 Avr 2020
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);
Bindhu PR
le 14 Avr 2020
Modifié(e) : Walter Roberson
le 15 Avr 2020
Bindhu PR
le 15 Avr 2020
Bindhu PR
le 15 Avr 2020
Walter Roberson
le 15 Avr 2020
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
Bindhu PR
le 17 Avr 2020
Bindhu PR
le 17 Avr 2020
Bindhu PR
le 28 Avr 2020
Walter Roberson
le 28 Avr 2020
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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!