Multiplication of function handles and integrating it

44 次查看(过去 30 天)
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 个评论
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