변수 인식 오류가 발생합니다.

24 次查看(过去 30 天)
성호 조
성호 조 2022-3-28
function F = comp(x)
F(1) = (x(1)+x(2)-x(6))*(2*x(1)+x(2)+x(3)+x(4))^2 - 2.937*10^5*(100-x(1)-x(3)+x(5)-x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^4;
F(2) = (x(1)+x(2)-x(6))*(2*x(1)+x(2)+x(3)+x(4)) - 1.046*10^7*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^3;
F(3) = (100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(2*x(1)+x(2)+x(3)+x(4)) - 0.02808*(100-x(1)-x(3)+x(5)-x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6));
F(4) = (2*x(1)+x(2)+x(3)+x(4)) - 5.555*10^5*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6));
F(5) = (100-x(1)-x(3)+x(5)-x(6)) - 1.978*10^6*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))^2;
F(6) = ((1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^2)*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))^2 - 1.166*10^(-9)*(x(1)+x(2)-x(6))*(100-x(1)-x(3)+x(5)-x(6));
end
function chem
fun = @comp;
x0 = [10,10,10,10,10,10];
x= fsolve(fun,x0)
CO = 100-x(2)+x(3)-x(4)-2*x(5)+2*x(6)
CO2 = 100-x(1)-x(3)+x(5)-x(6)
H2 = 1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6)
CH4 = x(1)+x(2)-x(6)
H2O = 2*x(1)+x(2)+x(3)+x(4)
C = x(4)+x(5)
end
실행하면
'x'은(는) 인식할 수 없는 함수 또는 변수입니다. 가 뜨면서 실행이 안됩니다. 방정식 풀고 싶은데 수정 가능할까요?

回答(1 个)

Raj
Raj 2023-10-3
编辑:Raj 2023-10-5
Hello 조 성호,
제 모국어는 한국어가 아니라서, 이 질문에 영어로 답변하려고 합니다. 이해해주셔서 감사합니다.
I understand that you are getting an error of “unrecognised variable” while running your code. I tried reproducing the error through your code. I found a workaround for you which you can try.
  • Instead of 2 functions ‘comp’ and ‘chem’ in a single file, try having function ‘comp’ in a separate file and call the file using ‘type’ command.
type comp.m
  • Inside ‘comp.m’ function file, omit using ‘end’ statement.
  • In the parent script file, omit using function ‘chem’ as it is unused and not being called anywhere in your code
type comp.m
fun = @comp;
x0 = [10,10,10,10,10,10];
x= fsolve(fun,x0)
CO = 100-x(2)+x(3)-x(4)-2*x(5)+2*x(6)
CO2 = 100-x(1)-x(3)+x(5)-x(6)
H2 = 1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6)
CH4 = x(1)+x(2)-x(6)
H2O = 2*x(1)+x(2)+x(3)+x(4)
C = x(4)+x(5)
For more information, refer to the MATLAB documentation of ‘fsolve’ function:
I hope this helps in your query!
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-10-5
There is no need to do that.
Just call the function chem directly and it runs properly, as you can see below.
OP just didn't know how to call the function.
chem
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 6.000000e+02.
x = 1×6
1.0e+05 * 0.9909 0.0035 1.4758 -3.4585 3.4591 0.9935
CO = 37.9096
CO2 = -0.0370
H2 = 1.0109e+03
CH4 = 96.9559
H2O = 262.1645
C = 65.1716
function F = comp(x)
F(1) = (x(1)+x(2)-x(6))*(2*x(1)+x(2)+x(3)+x(4))^2 - 2.937*10^5*(100-x(1)-x(3)+x(5)-x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^4;
F(2) = (x(1)+x(2)-x(6))*(2*x(1)+x(2)+x(3)+x(4)) - 1.046*10^7*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^3;
F(3) = (100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(2*x(1)+x(2)+x(3)+x(4)) - 0.02808*(100-x(1)-x(3)+x(5)-x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6));
F(4) = (2*x(1)+x(2)+x(3)+x(4)) - 5.555*10^5*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))*(1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6));
F(5) = (100-x(1)-x(3)+x(5)-x(6)) - 1.978*10^6*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))^2;
F(6) = ((1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6))^2)*(100-x(2)+x(3)-x(4)-2*x(5)+2*x(6))^2 - 1.166*10^(-9)*(x(1)+x(2)-x(6))*(100-x(1)-x(3)+x(5)-x(6));
end
function chem
fun = @comp;
x0 = [10,10,10,10,10,10];
x= fsolve(fun,x0)
CO = 100-x(2)+x(3)-x(4)-2*x(5)+2*x(6)
CO2 = 100-x(1)-x(3)+x(5)-x(6)
H2 = 1467-4*x(1)-3*x(2)-x(3)-x(4)+2*x(6)
CH4 = x(1)+x(2)-x(6)
H2O = 2*x(1)+x(2)+x(3)+x(4)
C = x(4)+x(5)
end

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!