필터 지우기
필터 지우기

Multiplication of function handles and integrating it

조회 수: 44 (최근 30일)
Ankush Kumar Mishra
Ankush Kumar Mishra 2022년 10월 17일
편집: Walter Roberson 2022년 10월 20일
clear all
clc
Nodes = 4;
Range_low = -pi();
Range_high = +pi();
Nodes_Size = (Range_high - Range_low)/(Nodes +1);
Nodes_Pos = [Range_low:Nodes_Size:Range_high];
j = 2;
q = @(x) (3 - sin(x)-sin(2*x)-cos(x));
f = @(x) (2*(e^(sin(x)))*(e^cos(x)));
for i = 1:(Nodes)
FEL{j} = @(x) (x - Nodes_Pos(i))/Nodes_Size;
FEL{j+1} = @(x) (Nodes_Pos(i+2) - x)/Nodes_Size;
FER{j} = @(x) (x - Nodes_Pos(i))/Nodes_Size;
FER{j+1} = @(x) (Nodes_Pos(i+2) - x)/Nodes_Size;
j = j+2;
end
FEL{1} = @(x) (Nodes_Pos(2) - x)/Nodes_Size;
FEL{end +1} = @(x) (x - Nodes_Pos(end-1))/Nodes_Size;
FER{1} = @(x) (2*(e^(sin(x)))*(e^cos(x)))*(Nodes_Pos(2) - x)/Nodes_Size;
FER{end +1} = @(x) (2*(e^(sin(x)))*(e^cos(x)))*(x - Nodes_Pos(end-1))/Nodes_Size;
FE = @(x) q*FEL{1}*FEL{1};
q = integral(FE, Nodes_Pos(1),Nodes_Pos(2))
Trying to integrate a function but multiplying function handles, is giving an error I tried '.*' as well, giving the same error shown below.
Operator '*' is not supported for operands of type 'function_handle'.
Error in main>@(x)q*FEL{1}*FEL{1} (line 26)
FE = @(x) q*FEL{1}*FEL{1};
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in main (line 28)
q = integral(FE, Nodes_Pos(1),Nodes_Pos(2))
Any suggestion on how to multiply three functions and do integration

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 17일
Multiplying function handles will never be supported by MATLAB. You need to invoke the handles on data and multiply the results.
FE = @(x) q*FEL{1}(x)*FEL{1}(x);
Reminder that the * operator is algebraic matrix multiplication and that integral() will always pass in a vector of x unless you use 'Arrayavalued' option. You should consider whether you really want to use * or if you want .* instead
  댓글 수: 2
Ankush Kumar Mishra
Ankush Kumar Mishra 2022년 10월 18일
That Worked. Thanks Walter.
FE = @(x) q(x)*FEL{1}(x)*FEL{1}(x);
Walter Roberson
Walter Roberson 2022년 10월 18일
If both of them are FEL{1} then you should probably use .^2 instead of evaluating the function twice.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by