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

4 ビュー (過去 30 日間)
Imaging3D
Imaging3D 2023 年 3 月 13 日
回答済み: Shubham 2023 年 5 月 3 日
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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by