필터 지우기
필터 지우기

What is wrong in my Fsolve m-file ????

조회 수: 2 (최근 30일)
kamal kiki
kamal kiki 2012년 2월 13일
when I saved the m-file below under the name trial.m
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 other names of variables.
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.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 13일
Variables that you define in the command window become part of the "base" workspace, and are not visible to the workspace of your function.
The fsolve documentation says,
Note: Passing Extra Parameters explains how to pass extra parameters to the system of equations, if necessary.
You should click on that link.
  댓글 수: 1
kamal kiki
kamal kiki 2012년 2월 13일
Thanks a million Walter Roberson,the documentation in the link is really helping.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by