How do I add a curve fitted plot to this code?

1 回表示 (過去 30 日間)
johnmurdock
johnmurdock 2019 年 4 月 8 日
回答済み: Sambit Senapati 2019 年 4 月 17 日
clc
clear
close all
xls=xlsread("Exam2_Data.xls");
t=xls(:,1);
y=xls(:,2);
SST=sum((y-mean(y)).^2);
for i=1:3
fprintf('For %d order fit\n\n',i);
disp('Polynomial coefficients are');
C=polyfit(t,y,i);
SSR=sum((y-polyval(C,t)).^2);
R2=1-SSR/SST;
fprintf('R^2 is %f\n\n',R2);
end
for i=2:length(t)-1
T=[t(i-1) t(i) t(i+1)];
Y=[y(i-1) y(i) y(i+1)];
if(t(i)<33.75&&t(i+1)>33.75)
C=polyfit(T,Y,2);
disp('Value at 33.75 is ');
polyval(C,33.75)
return
end
end
  2 件のコメント
Rik
Rik 2019 年 4 月 9 日
Do you mean like in a live script? Also, are you sure about the return? Don't you mean break? Return causes the current running function to return to the caller, while break causes the current (inner) loop to exit.
johnmurdock
johnmurdock 2019 年 4 月 9 日
I mean I want to add a block of code that plots a curve fitted line with an equation over the data I pulled from excel. I will try break instead of return as well.

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

回答 (1 件)

Sambit Senapati
Sambit Senapati 2019 年 4 月 17 日
you can use the polyval and plot functions in tandem to do the job here:
instead of calling polyval(C,33.75) at a single data point, do the following:
>> Y_new=polyval(C,T);
>> plot(T,Y_new)
You could do this for your original data set as well.

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by