Filter löschen
Filter löschen

Looping through a cell array to differentiate and integrate

1 Ansicht (letzte 30 Tage)
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);

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Feb. 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

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by