필터 지우기
필터 지우기

Fsolve function with values read from the workspace

조회 수: 3 (최근 30일)
kamal kiki
kamal kiki 2012년 2월 11일
답변: Victor Hugo Cantú 2019년 3월 25일
we all know that we use an m-file with the system of non-linear equations that we want to solve with Fsolve. I want the m-file that I have to use with Fsolve in order to solve the following system of non linear equations.
A.x^p1+2*y^2-5*x+7*y-40=0
3*x^2-y^2+4*x+B*y^p2-28=0
where x and y are the unknowns that must be given by the solution and A,B,p1 and p2 are values that must be read from the workspace.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 11일
xy0 = [1,1]; %initial guess
eqn = @(xy) [A .* xy(1).^p1 + 2 .* xy(2).^2 - 5 .* xy(1) + 7 * y(2) - 40, 3 .* xy(1).^2 - xy(2).^2 + 4 .* xy(1) + B .* xy(2).^p2 - 28];
xy = fsolve(eqn, xy0);
  댓글 수: 1
kamal kiki
kamal kiki 2012년 2월 12일
xy0 = [1,1]; %initial guess
eqn = @(xy) [A .* xy(1).^p1 + 2 .* xy(2).^2 - 5 .* xy(1) + 7 * y(2) - 40, 3 .* xy(1).^2 - xy(2).^2 + 4 .* xy(1) + B .* xy(2).^p2 - 28];
xy = fsolve(eqn, xy0);
Thank you very much Walter roberson.
When an m-file exactly like this is saved and run it is working.but when I saved the m-file below:
function fcns=trial(z)
x=z(1);
y=z(2);
fcns(1)=A.*x.^p1+2.*y.^2-5.*x+7.*y-40;
fcns(2)=3.*x.^2-y.^2+4.*x+B.*y.^p2-28;
end
and when I entered in the command window:
A=1
B=2
p1=2
p2=1
guess=[2 3]
and after that I entered in the command window:
result=fsolve(@trial,guess)
I received the following error message:
??? Undefined function or variable 'A'.
Error in ==> trial at 4
fcns(1)=A.*x.^p1+2.*y.^2-5.*x+7.*y-40;
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.
Please what is wrong in my m-file ? ( this same m-file is working when A,B,p1 and p2 are replaced by numbers in the m-file )
I am using m-files like this ones because working with x and y for me is easier than working with xy(1) and xy(2)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

kamal kiki
kamal kiki 2012년 2월 12일
xy0 = [1,1]; %initial guess eqn = @(xy) [A .* xy(1).^p1 + 2 .* xy(2).^2 - 5 .* xy(1) + 7 * y(2) - 40, 3 .* xy(1).^2 - xy(2).^2 + 4 .* xy(1) + B .* xy(2).^p2 - 28]; xy = fsolve(eqn, xy0);
Thank you very much Walter roberson.
When an m-file exactly like this is saved and run it is working.but when I saved the m-file below:
function fcns=trial(z)
x=z(1);
y=z(2);
fcns(1)=A.*x.^p1+2.*y.^2-5.*x+7.*y-40;
fcns(2)=3.*x.^2-y.^2+4.*x+B.*y.^p2-28;
end
and when I entered in the command window:
A=1
B=2
p1=2
p2=1
guess=[2 3]
and after that I entered in the command window:
result=fsolve(@trial,guess)
I received the following error message:
??? Undefined function or variable 'A'.
Error in ==> trial at 4
fcns(1)=A.*x.^p1+2.*y.^2-5.*x+7.*y-40;
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation.
FSOLVE
cannot continue.
Please what is wrong in my m-file ? ( the same m-file is working when A,B,p1 and p2 are replaced by numbers in the m-file )
I am using m-files like this one because working with x and y for me is easier than working with xy(1) and xy(2)
Also the m-file must have a name because I need it to use it when I am entering the f-solve command in the command window.

Victor Hugo Cantú
Victor Hugo Cantú 2019년 3월 25일
A = 1; B = 2; p1 = 2; p2 = 1;
guess = [2 3];
% x(1) = x; x(2) = y;
x = fsolve(@(x) trial(x,A,B,p1,p2), guess)
function F = trial(x,A,B,p1,p2)
F(1) = A*x(1)^p1 + 2*x(2)^2 - 5*x(1) + 7*x(2) - 40;
F(2) = 3*x(1)^2 - x(2)^2 + 4*x(1) + B*x(2)^p2 - 28;
end

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by