Can't integrate exponential function

2 ビュー (過去 30 日間)
Gabriel Sharp
Gabriel Sharp 2022 年 2 月 11 日
コメント済み: Gabriel Sharp 2022 年 2 月 11 日
I am trying to figure out how to write integral functions that have exponential numbers in the functions.
Matlab gives me an error for the following function:
%Practice integral with exponent
clc
clear
syms x
myFunc = @(x) (exp(x)/x);
y = integral(myFunc,3.5,4.5);
  1 件のコメント
Gabriel Sharp
Gabriel Sharp 2022 年 2 月 11 日
I get an error message:
Error using integralCalc/finalInputChecks (line 526)
Output of the function must be the same size as the input. If FUN is an array-valued
integrand, set the 'ArrayValued' option to true.

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

採用された回答

Matt J
Matt J 2022 年 2 月 11 日
編集済み: Matt J 2022 年 2 月 11 日
%syms x - doesn't belong here
myFunc = @(x) (exp(x)./x); %use ./
y = integral(myFunc,3.5,4.5)
y = 14.0083
  1 件のコメント
Gabriel Sharp
Gabriel Sharp 2022 年 2 月 11 日
What does the period do? It now works, by the way, thanks for the help.

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

その他の回答 (2 件)

Voss
Voss 2022 年 2 月 11 日
Telling integral() that the function is array-valued seems to work ok:
myFunc = @(x) (exp(x)/x);
y = integral(myFunc,3.5,4.5,'ArrayValued',true)
y = 14.0083
  2 件のコメント
Matt J
Matt J 2022 年 2 月 11 日
編集済み: Matt J 2022 年 2 月 11 日
Yes, but less efficiently. The integral computation will not be vectorized.
wp=linspace(3.5,4.5,1e5);
myFunc = @(x) (exp(x)/x);
tic;
y = integral(myFunc,3.5,4.5,'ArrayValued',true,'WayPoints',wp);
toc
Elapsed time is 2.280323 seconds.
myFunc = @(x) (exp(x)./x);
tic;
y = integral(myFunc,3.5,4.5,'WayPoints',wp);
toc
Elapsed time is 0.070478 seconds.
Voss
Voss 2022 年 2 月 11 日
I still do my integrals by hand, but @Gabriel Sharp may be interested in your comment.

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


Walter Roberson
Walter Roberson 2022 年 2 月 11 日
syms x
myFunc = (exp(x)/x);
tic; y1 = int(myFunc,3.5,4.5), double(y1), toc
y1 = 
ans = 14.0083
Elapsed time is 0.560509 seconds.
tic; y2 = vpaintegral(myFunc,3.5,4.5), double(y2), toc
y2 = 
14.0083
ans = 14.0083
Elapsed time is 0.159376 seconds.
  1 件のコメント
Gabriel Sharp
Gabriel Sharp 2022 年 2 月 11 日
Interesting how many ways the integral can be done!

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by