Difficulty in calling values from main program to fsolve function
조회 수: 3 (최근 30일)
이전 댓글 표시
my version is MATLAB 7.10.0(R2010a) My main program is aa=10; ab=12; ac=18; x0=[0;0]; result=fsolve(@myfun3,x0);
My function is myfun3 created as function F=myfun3(x) F=[2*x(1)^2+(x(2)*aa)+ab;5*x(2)^2+x(1)+ac]; end
the error shown is ??? Undefined function or variable 'aa'.
Error in ==> myfun3 at 2 F=[2*x(1)^2+(x(2)*aa)+ab;5*x(2)^2+x(1)+ac];
Error in ==> fsolve at 254 fuser = feval(funfcn{3},x,varargin{:});
Error in ==> yyyy at 5 result=fsolve(@myfun3,x0);
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
As i have sorted out the problem the function is not recognising the value of aa,ab and ac from the main problem.Why is that?????
댓글 수: 0
채택된 답변
Mischa Kim
2014년 3월 11일
편집: Mischa Kim
2014년 3월 11일
BITS, you need to pass the parameters ( aa, etc.) to the function:
function my_main()
aa = 10; ab = 12; ac = 18;
param = [aa,ab,ac]; % define parameter vector
x0 = [0;0];
f = @(x) myfun3(x,param); % define function (handle)
result = fsolve(f,x0);
disp(result)
end
function F = myfun3(x,param)
aa = param(1); % back out parameters from vector
ab = param(2);
ac = param(3);
F = [2*x(1)^2 + (x(2)*aa) + ab;...
5*x(2)^2 + x(1) + ac];
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!