フィルターのクリア

Looping through a cell array to differentiate and integrate

1 回表示 (過去 30 日間)
William
William 2024 年 2 月 24 日
回答済み: Walter Roberson 2024 年 2 月 24 日
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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by