user3223190
user3223190

Reputation: 153

Function as a parameter

I have a function in a .m file:

function [func diff1 diff2]=fun(x)

func=(3*x^3)+6;
diff1=(3*(x+0.00000001)^3-3*((x)^3))/0.00000001;
diff2=(3*((x+0.00000001)^3)-2*3*(x^3)+3*(x-.00000001)^3)/(.00000001^2);
end

In the second function I want to be able to pass in the function as a parameter. I keep getting

"Attempted to access fun(3); index out of bounds because numel(fun)=1."

Does anyone have any ideas?

function [x,N,fval]=halley(fun,guess,tol);
fval=fun(guess);
end

Upvotes: 0

Views: 103

Answers (1)

m.s.
m.s.

Reputation: 16334

You need to pass a function handle when calling halley:

halley(@fun, 3, 0.1)

Upvotes: 2

Related Questions