필터 지우기
필터 지우기

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 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