Multiplication of function handles and integrating it
70 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ankush Kumar Mishra
le 17 Oct 2022
Modifié(e) : Walter Roberson
le 20 Oct 2022
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
0 commentaires
Réponse acceptée
Walter Roberson
le 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 commentaires
Walter Roberson
le 18 Oct 2022
If both of them are FEL{1} then you should probably use .^2 instead of evaluating the function twice.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!