Clear Filters
Clear Filters

Multiplication of function handles and integrating it

44 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 17 Oct 2022
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 Comments
Ankush Kumar Mishra
Ankush Kumar Mishra on 18 Oct 2022
That Worked. Thanks Walter.
FE = @(x) q(x)*FEL{1}(x)*FEL{1}(x);
Walter Roberson
Walter Roberson on 18 Oct 2022
If both of them are FEL{1} then you should probably use .^2 instead of evaluating the function twice.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!