errors using Numerical Integration

6 vues (au cours des 30 derniers jours)
Elle Rae
Elle Rae le 5 Avr 2021
Commenté : Elle Rae le 5 Avr 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!

Réponse acceptée

the cyclist
the cyclist le 5 Avr 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.

Plus de réponses (1)

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

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by