errors using Numerical Integration

1 visualización (últimos 30 días)
Elle Rae
Elle Rae el 5 de Abr. de 2021
Comentada: Elle Rae el 5 de Abr. de 2021
I am trying to find the integral of the following function
y = @(x) 1/(sqrt(1+x.^4)); %function
a = -1; b = 1; %limits
x = a:.01:b;
S_int = integral(y,a,b); %integral function
S_trap = trapz(x,y);
fplot(y,[a,b])
xlabel('x'); ylabel('f(x)'); grid on
str1 = ['Integral of f(x) from ',num2str(a)];
str2 = [' to ',num2str(b),' is ',num2str(S)];
title([str1,str2])
fprintf('The integral to the function using the integral function is is %.6f\n', S_int);
fprintf('The integral to the function using the trapz function is is %.6f\n', S_trap);
function y = f(x)
y = 1/sqrt(1+x.^4);
end
I keep getting a myriad of errors that I'm not sure what to do about and would appreciate any help, Thank you!

Respuesta aceptada

the cyclist
the cyclist el 5 de Abr. de 2021
I see three errors:
(1) You need elementwise division instead of matrix division:
y = @(x) 1./(sqrt(1+x.^4)); % note the added dot
(2) For trapz, you need the numerical evaluation of y:
S_trap = trapz(x,y(x)); % Note the argument to y()
(3) There is no variable S. I assume you want one of your S_int or S_trap values.

Más respuestas (1)

David Hill
David Hill el 5 de Abr. de 2021
y = @(x) 1./(sqrt(1+x.^4)); %just need a dot (./)
a = -1; b = 1;
S_int = integral(y,a,b);

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by