I am getting the following error when I type in CalculateIntegral(2,5) into the MATLAB Command Window:
??? Error: File: CalculateIntegral.m Line: 2 Column: 1
Function definitions are not permitted at the prompt or in scripts.
I am not sure how to resolve this error. Thanks.
clear all;
function g = CalculateIntegral(s,N)
a=0;
b=1;
h=(b-a)/N;
x = 0:h:1;
g = ff(x).*exp(-s*x);
% compute the exact answer of the integral
exact_answer=antiderivative(b,s)-antiderivative(a,s);
% compute the composite trapezoid sum
If=0;
for i=1:(N-1)
If=If+g(i)*h;
end;
If=If+g(1)*h/2+g(N)*h/2;
If;
You can’t have a
clear allbefore your function definition (and you don’t need one). Just remove that first line to make your code work. MATLAB functions need to be by themselves in their own file, named like the function (CalculateIntegral.min your case).