필터 지우기
필터 지우기

getting function as an argument of function

조회 수: 7 (최근 30일)
Ali Isik
Ali Isik 2011년 11월 13일
hi, i am writing a matlab script that take function f which is a function of x and y and evaluate function f for some values of x and y. however i cannot get function, in my following code, f is seen as an array not a function. how can i get f. how can i evaluate at some points?
thanks.
function backEuler(f,x0,y0,h,N)
x(1)=x0;
y(1)=y0;
for i=1:N
x(i+1)=x(i)+h;
y_temp=y(i)+h*x(i)*x(i);
y(i+1)=y(i)+h*f(x(i+1),y_new);
end
plot(x,y);
end

채택된 답변

Naz
Naz 2011년 11월 13일
Let's say your function is f(x,y)=2x+y. You need to create this function prior you send it to your backEuler:
f=@(x,y)2*x+y;
backEuler(f,2,3,0,0);
The above lines go into the Command Window. Now, your backEuler function could be a separate file .m-function:
function backEuler(f,x0,y0,h,N)
x(1)=x0;
y(1)=y0;
for i=1:N
x(i+1)=x(i)+h;
y_temp=y(i)+h*x(i)*x(i);
y(i+1)=y(i)+h*f(x(i+1),y_new);
end
plot(x,y);
end
Just file->new->function, paste the above script and save it as backEuler (make sure that matlab does not have the function with the same name). Also, to use this function you need to set matlab to a current folder. That's it.
  댓글 수: 1
Naz
Naz 2011년 11월 13일
You can also call your function this way:
backEuler(@(x,y)x+y,2,3,0,0);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by