One problem specific to how you've written your code is that you're calling exp incorrectly. To compute the exponential of a variable z you need to use exp(z) instead of exp.^z.
In general:
- To integrate a function that returns numeric values, use the integral function.
f1 = @(x) x.^2;
result1 = integral(f1, 0, 1)
- To integrate a symbolic expression symbolically, use the int function.
syms x
f2 = x^2;
result2a = int(f2, x)
result2b = int(f2, x, 0, 1)
result2b1 = vpa(result2b)
result2b2 = double(result2b)
- To integrate a symbolic expression numerically, use one of two approaches. Either use vpaintegral or convert the symbolic expression into a function that returns numeric values using the matlabFunction function and then use the integral function.
syms x
helper = x^2;
f3 = matlabFunction(helper);
result3a = vpaintegral(helper, 0, 1)
result3b = integral(f3, 0, 1)
Best Answer