Function with double arguments

조회 수: 2 (최근 30일)
Claudia Santos
Claudia Santos 2016년 1월 31일
댓글: Walter Roberson 2016년 1월 31일
Can I make this?
function[g]= forward(f(x),x)
for j=5:5:40;
h=inv(j)
g=(f(x)+4*f(h)-f(h+h))/(h+h)
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 1월 31일
Do you mean something different for inv(j) than 1/j ?

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

채택된 답변

the cyclist
the cyclist 2016년 1월 31일
편집: the cyclist 2016년 1월 31일
You want to call a MATLAB function an argument that is itself a function, right? You can do that as follows. Define your function like this:
function [g] = forward(f,x)
for j=5:5:40;
h = inv(j);
g = (f(x)+4*f(h)-f(h+h))/(h+h);
end
end
and call it using a "function handle" like this:
forward(@sin,7) % Argument is the MATLAB built-in sine function
or like this
my_func = @(x) x^2; % Define your own function, and assign its handle
forward(my_func,7) % Argument is function defined by you
  댓글 수: 2
Claudia Santos
Claudia Santos 2016년 1월 31일
Oh thank you! Can you explain to me please this line of code?
my_func = @(x) x^2;
the cyclist
the cyclist 2016년 1월 31일
So, if you now do
my_func(5)
you will get 25 as output.
The best form of thanks is to upvote and/or accept solutions that helped you. This rewards the contributor, and can point future users to helpful answers.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by