Effacer les filtres
Effacer les filtres

Looping through a cell array to differentiate and integrate

1 vue (au cours des 30 derniers jours)
William
William le 24 Fév 2024
I am trying to build a matrix which is generated by integrating the sum of a set of derivatives of functions defined in a cell array. When I run the below code, it says there's an error in the 15th line saying that the variable x is not recognized. Any help would be greatly appreciated!
a = [0 1 1 0 0 1 1 0];
b = [0 0 1 1 0 0 1 1];
c = [0 0 0 0 1 1 1 1];
H = {@(x,y,z) (1/8).*(1-x).*(1-y).*(1-z), @(x,y,z) (1/8).*(1+x).*(1-y).*(1-z), @(x,y,z) (1/8).*(1+x).*(1+y).*(1-z), @(x,y,z) (1/8).*(1-x).*(1+y).*(1-z), @(x,y,z) (1/8).*(1-x).*(1-y).*(1+z), @(x,y,z) (1/8).*(1+x).*(1-y).*(1+z), @(x,y,z) (1/8).*(1+x).*(1+y).*(1+z), @(x,y,z) (1/8).*(1-x).*(1+y).*(1+z)};
for i = 1:8
for j = 1:8
f(i,j) = @(x,y,z) diff(H{1,i},x).*diff(H{1,j},x) + diff(H{1,i},y).*diff(H{1,j},y) + diff(H{1,i},z).*diff(H{1,j},z);
k(i,j) = integral3(f(i,j),min(a),max(a),min(b),max(b),min(c),max(c));
end
end
Incorrect number or types of inputs or outputs for function diff.

Error in solution>@(x,y,z)diff(H{1,i},x).*diff(H{1,j},x)+diff(H{1,i},y).*diff(H{1,j},y)+diff(H{1,i},z).*diff(H{1,j},z) (line 7)
f(i,j) = @(x,y,z) diff(H{1,i},x).*diff(H{1,j},x) + diff(H{1,i},y).*diff(H{1,j},y) + diff(H{1,i},z).*diff(H{1,j},z);

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Fév 2024
There are two major functions diff()
When at least one of the parameters to diff() is a symbolic expression, or a symbolic function, or a symbolic array, then the result of diff() is symbolic differentiation .
If none of the parameters to diff() are symbolic expressions, or symbolic functions, or symbolic arrays, then the operation of diff is along the lines of A(2:end) - A(1:end-1) (but possibly repeated several times, depending on the parameters.)
You are attempting to take diff(H{1,i},x) where H{1,i} is an anonymous function, and x is a numeric parameter. That fails.
a = [0 1 1 0 0 1 1 0];
b = [0 0 1 1 0 0 1 1];
c = [0 0 0 0 1 1 1 1];
syms x y z
H = {(1/8).*(1-x).*(1-y).*(1-z), (1/8).*(1+x).*(1-y).*(1-z), (1/8).*(1+x).*(1+y).*(1-z), (1/8).*(1-x).*(1+y).*(1-z), (1/8).*(1-x).*(1-y).*(1+z), (1/8).*(1+x).*(1-y).*(1+z), (1/8).*(1+x).*(1+y).*(1+z), (1/8).*(1-x).*(1+y).*(1+z)};
for i = 1:8
for j = 1:8
f{i,j} = matlabFunction(diff(H{1,i},x).*diff(H{1,j},x) + diff(H{1,i},y).*diff(H{1,j},y) + diff(H{1,i},z).*diff(H{1,j},z), 'vars', [x, y, z]);
k(i,j) = integral3(f{i,j},min(a),max(a),min(b),max(b),min(c),max(c));
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by