Error using lsqcurvefit: Function value and YDATA sizes are not equal.

7 ビュー (過去 30 日間)
Max De Chastelain
Max De Chastelain 2021 年 4 月 4 日
回答済み: Matt J 2021 年 4 月 4 日
I am trying to use the lsqcurvefit function to fit a sine wave to a data set, however i get the following error:
Function value and YDATA sizes are not equal.
when running the following code:
A = 0.3;
omega = 1;
phi = 0.1;
c = 0.1;
constants = [A omega phi c];
fun = @(timearray,constants) A .* (sin((timearray .* omega) + phi)) + c;
fittedconstants = lsqcurvefit(fun,constants,timearray,anglearray);
plot(timearray,fun(timearray,fittedconstants));
The array "timearray" is a 1x10001 double, defined by:
timearray = 0:0.01:100;
and the "anglearray" is also a 1x1001 double, filled with experimental data.
I feel as if it may be due to the mathematical operators used in my function, I have tried removing the "." before the "*" but this doesn't seem to work. placing a "." before and after the "sin" operator yields more errors.
Cheers in advance, I hope the information given is enough for a solution.
  1 件のコメント
Max De Chastelain
Max De Chastelain 2021 年 4 月 4 日
Checking the size of "fun" and "anglearray" yields the following:
>> size(fun)
ans =
1 1
>> size(anglearray)
ans =
1 10001
I am unsure as to how to get the size of "fun" to be a 1x10001 double.

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

採用された回答

Matt J
Matt J 2021 年 4 月 4 日
A = 0.3;
omega = 1;
phi = 0.1;
c = 0.1;
initialGuess = [A omega phi c];
fun = @(x,timearray) x(1) .* (sin((timearray .* x(2)) + x(3))) + x(4);
fittedconstants = lsqcurvefit(fun,initialGuess,timearray(:).',anglearray(:).');
plot(timearray,fun(fittedconstants, timearray));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by