필터 지우기
필터 지우기

Plotting normal curve and a third order polynomial

조회 수: 3 (최근 30일)
daniel
daniel 2013년 5월 27일
Hello, i'm new with matlab and i have a problem with my code, i got this
y=[70,73,62,67,70,72,67,72,71,73,70,75,66,69,67,72,67,69,71,71,68,74,73,62,65,75,67,64,72,73];
max(y);
min(y);
bins=min(y):max(y);
numel(y);
x=0:1:29;
numel(x);
polyfit(x,y,2);
best_y=-0.0030*x.^2+0.0851*x+691891;
%polyfit(x,y,3);
%best_y=(0.0011*x.^3-0.0499*x.^2+0.6198*x+68.0076);
%plot(x,best_y);
u=mean(y);
s=std(y);
normal_y=30*((exp((-(x-u).^2)/(2*s.^2)))/(s*(2*3.1416).^(0.5)));
plot(x,best_y,x,normal_y);
my first tried was with the third order polynomial and then i tried it with the second order one. when i got the graph it looked like 2 parallel lines , i know they have to be close. i dont know what is the problem with my code
thanks a lot

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 27일
편집: Azzi Abdelmalek 2013년 5월 27일
The problem is this line
best_y=-0.0030*x.^2+0.0851*x+691891;
It should be
best_y=-0.0030*x.^2+0.0851*x+69.1891;
%or
v=polyfit(x,y,2);
best_y=sum(bsxfun(@times,[x.^2;x;ones(1,numel(x))],v'))
plotyy(x,y,x,best_y)
%or for order 3
v=polyfit(x,y,3);
best_y=sum(bsxfun(@times,[x.^3; x.^2;x;ones(1,numel(x))],v'))
plotyy(x,y,x,best_y)
  댓글 수: 2
David Sanchez
David Sanchez 2013년 5월 27일
p = polyfit(x,y,2); % p is an array with polynomial coefficient
best_y = p(1)*x.^2+p(2)*x+p(3); %
daniel
daniel 2013년 5월 27일
hello Azzi thanks for the reply.
i fixed "691891" now the top looks kinda curved but still they are apart like one on top of the graph and the other one at the bottom.
And.
best_y=sum(bsxfun(@times,[x.^2;x;ones(1,numel(x))],v'))
Well as i mentioned im really new with matlab so i got lost with that code hehe.( but thanks a lot )
the curious part is that when i plot the best_y by itself it looks good upside-down parabola) , same with the normal_y which looks like half of the normal curve. but as soon as i plotted them together one is on top semi-curve and the other one is a line at the bottom. is that ok ?
thanks guys,

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


David Sanchez
David Sanchez 2013년 5월 27일
what's your y data? Your code is full of useless stuff and "magic" numbers.
Add this at the end of your code:
axis([0 30 69 70])
You'll see the Gaussian shape you look for
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 27일
David, maybe you are answering another question.

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

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by