Looping through a cell array to differentiate and integrate

1 次查看(过去 30 天)
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);

采纳的回答

Walter Roberson
Walter Roberson 2024-2-24
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by