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 件のコメント
Elle Rae
Elle Rae 2021 年 4 月 5 日
Thank you!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by