I'm trying to call a a function that allows the user to input an equation of their choosing, and then use a separate function to approximate it using the trapezoidal rule in numerical analysis.
Here's my code for the user input equation:
function f = myf(x)
y=@(x) input('Input Equation: ');
f=y(x);
end
And here's the code for applying the trapezoidal rule:
function T=trapez(f,a,b,n)
h=(b-a)/n;
x=[a+h:h:b-h];
T=h/2*(feval(f,a)+feval(f,b)+2*sum(feval,f,x));
fprintf('The approximation by trapezoida rule is: %f with step h: %f\n',T,h);
end
My issue is trying to use the equation determined by the first function as an input in the second.
>> f=myfun
Input Equation: exp(x^2)
f =
@(x)exp(x^2)
f =
@(x)exp(x^2)
>> trapez(f,0,1,15)
Error using feval
Not enough input arguments.
Error in trapez (line 4)
T=h/2*(feval(f,a)+feval(f,b)+2*sum(feval,f,x));