Fit multiple curves in one graph using least squares

63 ビュー (過去 30 日間)
NMans
NMans 2018 年 5 月 10 日
回答済み: Szu-Yu Lee 2021 年 4 月 8 日
Hi all,
I've created yearly power curve for multiple wind farms (with multiple curves in a graph) and I would like to fit these individually - how do I do that? The code is very long for me to copy and paste here but I've used
for yrs = 1: length(years)
and so on..
and then plot(Wind_speed(:,yrs), Pow_ave(:,yrs), '*');
hold on;
and so on...
Thanks!
  3 件のコメント
NMans
NMans 2018 年 5 月 10 日
Thanks Stephan - however this is plotting multiple curves plus fitting onto one graph. I already have multiple curves in my graph using the for function for every unique year. My question how do I fit each individual curve in my graph. The reason for doing this is I've written my code for 14 wind farms, so my code produces 14 graphs with each one having multiple curves. I should have made that clear.
Stephan
Stephan 2018 年 5 月 11 日
Hi,
see my new answer - does this help you? Or did I misunderstand you?
Best regards
Stephan

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

回答 (4 件)

Stephan
Stephan 2018 年 5 月 10 日
編集済み: Stephan 2018 年 5 月 10 日
Hi,
use the
fit
function for this - see here:
You can take the several x,y-pairs and fit them individually with this function. Same result you get by using curve fitting app.
Result could look like this (all taken from mathworks.com ):
or for example this
depending on the fitting function you choose.
Best regards
Stephan

Cathal Cunningham
Cathal Cunningham 2018 年 5 月 10 日
You could try fit (allows polynomial line fitting amongst others) or glmfit (Generalized Linear Model Fit) which allows you to define what kind of fit you would like to use.

Stephan
Stephan 2018 年 5 月 10 日
編集済み: Stephan 2018 年 5 月 11 日
Hi,
use this example for your purposes:
% load some example data
load census
% create second sata set
cdate2 = cdate;
pop2 = pop * 0.5;
% fit 2 curves
curvefit1 = fit(cdate,pop,'poly2');
curvefit2 = fit(cdate2,pop2,'poly2');
% create new data from fit objects
X = 1790:1990;
Y1 = curvefit1(X);
Y2 = curvefit2(X);
% plot measured data
a = subplot(2,1,1);
plot(cdate,pop, '*', 'color', 'r')
hold on
plot(cdate2,pop2, 'o', 'color', 'b')
% Plot only the trend lines
b = subplot(2,1,2);
plot(X, Y1, 'color', 'r')
hold on
plot(X, Y2, 'color', 'b')
% same scaling of the subplots
linkaxes([a b], 'xy')
hold off
this gives you:
.
The code snippet:
% fits 2 curves
curvefit1 = fit (cdate, pop, 'poly2');
curvefit2 = fit (cdate2, pop2, 'poly2');
adjusts the curves for both records individually. If I understand you correctly, that's what you want to do:
Calculate an individual curve adaptation for each individual year and repeat this for all 14 wind turbines. For example, if you look at ten years, you'll get a total of 14 charts, each with 10 individually adjusted curves. So in this case you need 140 times the fit function.
Correct?
What you have to do is hand over the corresponding X, Y pairs for each year to the fit function and repeat this process for a new year with a new curve fit.
To execute this in a for loop you could use this code snippet:
% load example data
load census
% preallocate an Array with number of fits you need as columns - here 2
popu = zeros(length(cdata),2);
% fill the data in:
popu(:,1) = pop;
popu(:,2) = pop * 0.5;
% collect all individual fitted curve objects in a cell array
for i = 1:2
Data{i} = fit(cdate,popu(:,i),'poly2');
end
.
Doing so, will give you a cell Array containing for example 140 individual fits, which you can plot as shown above or like you need.
.
Best regards
Stephan
  3 件のコメント
Stephan
Stephan 2018 年 5 月 15 日
Hi,
hope it works well for your purpose.
Best regards
Stephan
Stephan
Stephan 2018 年 5 月 17 日
Still not happy?

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


Szu-Yu Lee
Szu-Yu Lee 2021 年 4 月 8 日
Hi, I guess you are trying to fit a large number of independent equations? MATLAB "fit" function do not support multiple independent curves fitting. I have created a function that takes in a 2D matrix, where each row is a curve to be fitted to some polynomial expression. The function does not use for-loop, so can work on large number of rows in a very short time.
https://www.mathworks.com/matlabcentral/fileexchange/90017-matrix-row-wise-polynomial-fit

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by