errors using Numerical Integration

6 次查看(过去 30 天)
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!

采纳的回答

the cyclist
the cyclist 2021-4-5
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.

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by