フィルターのクリア

error: matrix dimensions must agree

1 回表示 (過去 30 日間)
sxh
sxh 2023 年 6 月 23 日
コメント済み: sxh 2023 年 6 月 23 日
Hi
I am banging my head over this least square curve fitting even when following the simplest procedure I found. The error says,
"Error using /
Matrix dimensions must agree." - in line where the function 'func' is defined. Anyone have answers?
Thanks
clc
close all
clear
background = csvread(['C:\Users\shadh\Downloads\vnaSweep\ar2000mT\5.5mm length\0.1mmReCal\0w\0SM.csv'],3,0,[3,0,1603,2]);
backgroundImpIm = background(:,3);
f = background(:,1);
freq = f(100:1601);
x0 = [1,1];
h = 5E-3;
a = 5E-5;
m_e = 9.1E-31;
q = 1.6E-19;
c = 3E8;
omega = 2*pi*freq;
k_0 = omega/c;
beta = k_0;
epsilon_real = 1;
epsilon_im = 0;
x = epsilon_im./epsilon_real;
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h*(1 + 0.19/((K_a/60 +1) - 0.81))/c)));
K_a = lsqcurvefit(func,x0,freq,backgroundImpIm(100:1601))
  2 件のコメント
KSSV
KSSV 2023 年 6 月 23 日
ONly one csv file is uploaded....error line is not mentioned.
sxh
sxh 2023 年 6 月 23 日
Sorry about that. I just edited the code..

サインインしてコメントする。

採用された回答

Torsten
Torsten 2023 年 6 月 23 日
編集済み: Torsten 2023 年 6 月 23 日
K_a in your function definition is a scalar. Thus you have to change x0 to be a scalar, too.
clc
close all
clear
background = csvread(['0SM.CSV'],3,0,[3,0,1603,2]);
backgroundImpIm = background(:,3);
f = background(:,1);
freq = f(100:1601);
x0 = 1;
h = 5E-3;
a = 5E-5;
m_e = 9.1E-31;
q = 1.6E-19;
c = 3E8;
omega = 2*pi*freq;
k_0 = omega/c;
beta = k_0;
epsilon_real = 1;
epsilon_im = 0;
x = epsilon_im./epsilon_real;
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h.*(1 + 0.19./((K_a/60 +1) - 0.81))/c)));
K_a = lsqcurvefit(func,x0,freq,backgroundImpIm(100:1601))
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
K_a = 48.3478
  1 件のコメント
sxh
sxh 2023 年 6 月 23 日
Youve got good eyes mate. Thanks. Spent almost a full day for the error, wonder how I missed this one.
Thanks a bunch,
S

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2023 年 6 月 23 日
編集済み: James Tursa 2023 年 6 月 23 日
I would presume you may need element-wise operators. Try this:
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h.*(1 + 0.19./((K_a/60 +1) - 0.81))/c)));
  3 件のコメント
James Tursa
James Tursa 2023 年 6 月 23 日
Type the following at the command line:
dbstop if error
Then run your code. When the error happens, the program will pause with all variables intact. Examine them to figure out which variables are causing the dimension problem, then backtrack in your code to figure out why the dimensions are not what you expected.
sxh
sxh 2023 年 6 月 23 日
The only culprit I can think of is 'K_a' but this is the one I am trying to solve for.
I replaced 'K_a' with just a scalar value 1 to see if the function value the same as the size of the YDATA. They are the same size.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by