필터 지우기
필터 지우기

[Fittype using an anonymous function] Can the independet variables be indices for an array?

조회 수: 4 (최근 30일)
Is there a way to use the independent variables as indices for an array?
For example, in the following example, 'shape' is a 2D array, and x and y are used as indices, e.g., shape(x,y).
ft = fittype(@(nB,h,shape,lambda,nA,x,y) ...
2*pi/lambda*abs(nA-nB)*shape(x,y)*h, ...
'dependent','z','independent',{'x','y'},...
'problem',{'shape','lambda','nA'},'coefficients',{'nB','h'});
I get the following error:
Error using fittype>iAssertIsMember (line 1084)
Problem parameter shape does not appear in the equation expression.
I tried to define another function to access the array.
getElementOf = @(array,ii,jj) array(ii,jj);
ft = fittype(@(nB,h,shape,lambda,nA,x,y) ...
2*pi/lambda*abs(nA-nB)*getElementOf(shape,x,y)*h, ...
'dependent','z','independent',{'x','y'},...
'problem',{'shape','lambda','nA'},'coefficients',{'nB','h'});
When defining the equation '2*pi/lambda*abs(nA-nB)*getElementOf(shape,x,y)*h' in a separate anonymous function, I get a correct result; however, its use in the fittype definition produces an error:
Error using fittype/testCustomModelEvaluation (line 12)
Expression 2*pi/lambda*abs(nA-nB)*getElementOf(shape,x,y)*h is not a valid MATLAB expression, has non-scalar
coefficients, or cannot be evaluated:
Error while trying to evaluate FITTYPE function :
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Thanks.
  댓글 수: 2
Imaging3D
Imaging3D 2023년 3월 13일
편집: Imaging3D 2023년 3월 13일
Thank you for the reply. Could you please elaborate what you meant by using interpolation?
getElementOf = @(array,ii,jj) array(ii,jj);
ft = fittype(@(nB,h,shape,lambda,nA,x,y) ...
2*pi/lambda*abs(nA-nB)*interp2(shape,x,y)*h, ...
'dependent','z','independent',{'x','y'},...
'problem',{'shape','lambda','nA'},'coefficients',{'nB','h'});
When I modify the code as above, I get the error below. The expression gives a correct result within a separately defined anonymous function, but it gives an error when used in the fittype definition.
Expression 2*pi/lambda*abs(nA-nB)*interp2(shape,xx,yy)*h is not a valid MATLAB expression, has non-scalar
coefficients, or cannot be evaluated:
Error while trying to evaluate FITTYPE function :
Interpolation requires at least two sample points for each grid dimension.

댓글을 달려면 로그인하십시오.

답변 (1개)

Shubham
Shubham 2023년 5월 3일
Hi Imaging3D,
It is very well possible to use independent variables as indices for an array. But it seems in your code, the issue is not related to using independent variables as indices.
The error message suggests that the problem parameter 'shape' is not appearing in the equation expression. This means that 'shape' is not being passed correctly to the fittype function.
Make sure that the 'shape' variable is defined and passed correctly to the fittype function. You can also try removing the 'problem' parameter and passing the 'shape' variable as an independent variable, like this:
ft = fittype(@(nB,h,lambda,nA,x,y,shape) ...
2*pi/lambda*abs(nA-nB)*shape(x,y)*h, ...
'dependent','z','independent',{'x','y','shape'},...
'coefficients',{'nB','h'});
This should allow you to use 'shape' as an independent variable and access its elements using 'x' and 'y' as indices.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by