Hi!
The first step was to 'read' an excel file, with each column being x,y and e, respectively;
>> xlsread('Task3excelspreadsheet.xlsx', 'Sheet1')
ans =
0.0022 4.1400 0.1900
0.0071 4.3400 0.1700
0.0220 4.4100 0.1000
0.0320 4.2200 0.0500
0.0500 4.2500 0.0500
0.0710 4.0500 0.0500
0.1000 3.9000 0.0200
0.1580 3.6040 0.0095
0.2240 3.3670 0.0080
I then went to plot it;
>> x = ans(1:17,1); >> y = ans(1:17,2); >> e = ans(1:17,3); >> errorbar(x,y,e)
All was good. The plot is shown in the image attached.
Now, i'm attempting the second part of the question, which reads "Use the polyfit function to fit a second order (quadratic) and a third order (cubic) polynomial to this data. Plot the data and the fit on the same figure (the hold function may be useful)".
Can anyone please help? Would be greatly appreciated!
Oliver .T.

답변 (2개)

Star Strider
Star Strider 2016년 8월 23일

0 개 추천

It’s difficult to describe it without actually writing the code for you. The documentation for the polyfit, polyval, and hold functions should explain this well enough for you to figure it out yourself.
If you’re still stuck after reading those, I’ll post the complete code.

댓글 수: 2

Oliver Trembearth
Oliver Trembearth 2016년 8월 23일
I've had a go, but i'm only getting a singular graph; not sure why. Thanks so much for your help... :)
>> a = polyfit(x,y,3);
>> hold on
>> x1 = linspace(min(x),max(x));
>> y1 = polyval(a,x1,2);
>> plot(x1,y1,'r')
>> y2 = polyval(a,x1,3);
>> hold on
>> plot(x1,y2,'m');
My pleasure.
The linspace call is a good idea. You get a smooth line with it.
You have the essence of the necessary code. I renamed your variables a bit to make them somewhat more obvious. It’s always good to use variable names that are descriptive of what you’re doing. It makes keeping track of them easier.
Please put your code in a script file (with a .m extension) and run that file rather than running your code entirely from the Command Window. It makes life easier, and you can submit the script file as part of your homework, rather than copying and pasting the Command Window commands. If you need some help with this, see the relevant sections in Getting Started.
Coding is individual and everyone has their own style. I do all the parameter estimations (using polyfit here) and function fit calculations (using polyval) first, then do the plots in another block of code. I would rearrange your code a bit:
xv = linspace(min(x),max(x));
a2 = polyfit(x,y,2);
y2 = polyval(a2,xv);
a3 = polyfit(x,y,3);
y3 = polyval(a3,xv);
figure(1)
errorbar(x,y,e)
hold on
plot(xv, y2, 'r')
plot(xv, y3, 'm')
hold off
grid
title( ... )
xlabel( ... )
ylabel( ... )
That’s similar to the code I wrote, and it should be a bit easier to read. (I’m not posting my code because with these slight changes, yours should work without error.)
Note that polyval only takes two arguments in your code here. The third and fourth arguments are specific outputs from polyfit (that you don’t need with your data). They involve centering and scaling of the input data to get an acceptable fit in unusual situations.

댓글을 달려면 로그인하십시오.

KSSV
KSSV 2016년 8월 23일

0 개 추천

clc; clear all ;
A = [0.0022 4.1400 0.1900
0.0071 4.3400 0.1700
0.0220 4.4100 0.1000
0.0320 4.2200 0.0500
0.0500 4.2500 0.0500
0.0710 4.0500 0.0500
0.1000 3.9000 0.0200
0.1580 3.6040 0.0095
0.2240 3.3670 0.0080];
x = A(:,1) ; y = A(:,2) ; e = A(:,3) ;
errorbar(x,y,e,'k') ;
p = polyfit(x,y,3) ;
hold on
x1 = linspace(min(x),max(x)) ;
%%Second order polynomial
y1 = polyval(p,x1,2);
plot(x1,y1,'r') ;
%%third order polynomial
y2 = polyval(p,x1,3);
hold on
plot(x1,y2,'m') ;

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

질문:

2016년 8월 23일

댓글:

2016년 8월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by