fitting three lines in one figure
5 views (last 30 days)
Show older comments
Hi,
I am trying to fit three lines in one figure. Each of these lines has its own x and y value. I first created a data set for my x values versus y. Then I fitted themial in an exponential curve. Then I generated an m.file with one of the fitting. I tried to edit the code in order to allow for the other two fits be included, however, this did not work with me. The code is shown below. Can someone please let me know what is wrong with the code?
% --- Plot data originally in dataset "y vs. x"
f_ = clf;
figure(f_);
legh_ = []; legt_ = {}; % handles and text for legend
xlim_ = [Inf -Inf]; % limits of x axis
ax_ = subplot(1,1,1);
set(ax_,'Box','on');
axes(ax_);
hold on;
x = [0.8 1.4 2 2.6 3.2 3.8 4.4 5 5.6 6.2];
y = [946.51 920 885.46 859.99 828.93 804.33 773.89 752.11 723.53 701.80];
x2 = [0.99 2.4 3.81 5.22 6.63 8.04 9.45 10.86 12.27 13.68 15.09 16.5];
y2 = [277.93 257.34 237.91 219.98 203.74 188.21 173.07 160.73 148.23 137.81 126.80 116.51];
x3 = [0.8 1.15 1.5 1.85 2.2 2.55 2.9 3.25 3.6 3.95 4.3 4.65 5 5.35 5.7 6.05 6.4 6.75 7.1 7.45];
y3= [959.67 941.02 926.95 909.74 885.90 868.08 854.90 838.49 815.41 799.75 788 772.78 752.02 739.62 725.93 712.40 693.03 679.29 667.88 655.83];
h_ = line(x, y,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x));
xlim_(2) = max(xlim_(2),max(x));
legh_(end+1) = h_;
legt_{end+1} = 'y vs. x';
% --- Create fit "fit 1"
fo_ = fitoptions('method','NonlinearLeastSquares','Algorithm','Levenberg-Marquardt');
st_ = [1.000 -0.00 ];
set(fo_,'Startpoint',st_);
ft_ = fittype('exp1' );
% Fit this model using new data
cf_ = fit(x,y,ft_ ,fo_);
cf_1 = fit(x2,y2,ft_,fo_);
cf_2 = fit(x3,y3,ft_,fo_);
h_ = plot(cf_,'fit',0.95);
hold on;
h_1 = plot(cf_1,'fit',0.95);
holf on;
h_2 = plot(cf_2,'fit',0.95);
%plot(t,sin(t),'-.r*')
%set(findobj(gca,'Type','line','Color',[0 1 0]),... %'Color','green',... %'LineWidth',2);
legend off; %
Any help we be appreciated.
Best wishes
AA
1 Comment
Image Analyst
on 28 Aug 2012
Edited: Image Analyst
on 30 Aug 2012
What does " themial " mean? And please add whatever toolbox fitoptions() is in to the "Products" tag. I don't have that function so I can't help you further.
Answers (1)
Davide Ferraro
on 30 Aug 2012
Hi,
suggestion: write also the error you obtain:
Error using fit>iFit (line 129) X must be a matrix with one or two columns.
Error in fit (line 109) [fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in test (line 29) cf_ = fit(x,y,ft_ ,fo_);
This may help people in providing you an answer quickly although they do not have the toolbox.
Your variable should be a matrix with one or two columns. You only need to transpose your variables from a row to a vector:
% Fit this model using new data
cf_ = fit(x',y',ft_ ,fo_);
cf_1 = fit(x2',y2',ft_,fo_);
cf_2 = fit(x3',y3',ft_,fo_);
You also have a typo later "holf on" to fix to run the code without errors.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!