Problem with integral() function
Show older comments
I have a problem with the usage of the MatLab build-in integral function and I cannot understand the essence of it. Below I am attaching the things that might be the cause of a given problem. I have also implemented the Trapezoidal and the Simson's rules and they work completly fine, so that is the thing that makes me even more confused as I guess there should not be a problem with the 'f' function.
cheby1 - function that generates Chebyshev polynomials of the first order
fibonacci - my own function that generates the Fibonacci sequence
MATLAB version - R2018b
main.m
n = -1;
while(n <= 0)
n = input('Enter the n number: ');
end
fib = fibonacci(n);
fun = @(x) f(n,x,fib);
fx = f(n,x,fib);
disp('Simpson:')
S = simpson(0,4,0.01,fun)
disp('Trapezoidal:')
T = trapezoidal(0,4,0.01,fun)
disp('MatLab build-in')
I = integral(fun,0,4)
chey1.m
function T = cheby1(x, N)
if N == 1
T(N) = x;
else
T(1) = x;
T(2) = 2*x^2-1;
for i = 3:N
T(i) = 2*x*T(i-1)-T(i-2);
end
end
end
f.m - function that generates f(x) of the form
where
is the Chebyshev polynomial
function S = f(n,x,fib)
S = 0;
T = cheby1(x, n);
for i=1:n
S = S + T(i) * fib(i);
end
end
The erros I get:

Accepted Answer
More Answers (1)
Star Strider
on 28 May 2019
Your post lacks details. However, Iit seems that ‘x’ is a vector. It is not possible to assign a vector to a scalar array element.
Try this:
function T = cheby1(x, N)
if N == 1
T(N) = x;
else
T(1) = x(1);
T(2) = 2*x(2)^2-1;
for i = 3:N
T(i) = 2*x(i)*T(i-1)-T(i-2);
end
end
end
That may not be the only error. If I guess correctly, it should prevent the error you posted.
1 Comment
Jakub Osek
on 28 May 2019
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!