errors using Numerical Integration

조회 수: 1 (최근 30일)
Elle Rae
Elle Rae 2021년 4월 5일
댓글: Elle Rae 2021년 4월 5일
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