How to integrate anonymous functions

26 ビュー (過去 30 日間)
Robert  Flores
Robert Flores 2018 年 4 月 24 日
編集済み: John D'Errico 2018 年 4 月 25 日
Hello,
I am trying to integrate my area function A(x) in my code, but it keeps telling me I need to put a "handle" on it. I thought putting bounds would be sufficient, I guess I was wrong. If you can help me, great, below is a a copy of my code. An image of what I am trying to integrate is in the attachments.
Code:
% Calculus way (True Values)
E = 30*10^6;
F = 300;
SLOPE = -0.25/6;
D = @(x) (SLOPE*x)+1; % This is the function of Diameter
A = @(x) (pi/4)*(D(x))^2; % This is the function of Area
True_Area = integral(F/(E*A),0,6)

回答 (2 件)

Guillaume
Guillaume 2018 年 4 月 24 日
You cannot combine numbers (F and E) and functions with mathematics operations. You need to create a new function:
True_Area = integral( @(x) F./(E*A(x)), 0, 6);
The @(x) F./(E*A(x)) is this new function.
  2 件のコメント
Robert  Flores
Robert Flores 2018 年 4 月 24 日
I copied your script and it still didn't run, sorry for the late response.
Robert  Flores
Robert Flores 2018 年 4 月 25 日
True_Extension = integral(EXT_FUN,0,6,'ArrayValued',true);
This is what worked

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


John D'Errico
John D'Errico 2018 年 4 月 25 日
編集済み: John D'Errico 2018 年 4 月 25 日
It seems you understand how to form A(x), from D(x). That is, you knew to use D(x) in there, when you wrote the function handle A. You also know to use the .^ operator.
So why did you forget everything you knew when you wrote the call to integral? :)
True_Area = integral(@(x) F./(E*A(x)),0,6)
True_Area =
0.000101859163578813
You need to form an anonymous function handle, evaluating A(x) inside it. And since you are dividing that into a constant, and since x will be a vector when called by integral, you need to use the ./ operator.
To verify the result:
syms x
D = SLOPE*x+1;
A = (pi/4)*D^2;
True_Area = int(F/(E*A),0,6)
True_Area =
1/(3125*pi)
vpa(True_Area)
ans =
0.00010185916357881301489208560855841

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by