![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172154/image.png)
Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
i really need help with Matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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+69.1891;
%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);
plot(x,best_y)
plot(x,normal_y)
plot(x,best_y,x,normal_y)
My first try 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 don't know what is the problem with my code.
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 a lot
0 commentaires
Réponses (1)
Image Analyst
le 27 Mai 2013
Modifié(e) : Image Analyst
le 27 Mai 2013
The problem was you were not accepting the calculated coefficients from your fit (polyfit) and doing anything with them (like calling polyval). Try this:
clc;
fontSize = 20;
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);
plot(x, y, 'bo');
hold on;
grid on;
coeffs = polyfit(x,y, 3);
fitted_y = polyval(coeffs, x);
plot(x, fitted_y, 'rs-', 'LineWidth', 3);
title('Y vs. X', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172154/image.png)
2 commentaires
Image Analyst
le 28 Mai 2013
Modifié(e) : Image Analyst
le 28 Mai 2013
I think you're getting confused, or I am. That y looks like it's a constant with some Gaussian noise added to it, so y = 69.56 +/- 3.57. So you can't just plot y against x and expect to see a Gaussian shape! Imagine you had a million y and plotted them. They're all going to be around 69.56 and bounce around that for a million points that you plotted. It's going to look like a noisy flat line.Doesn't sound like what you want.
What you can do is to take the histogram of y and plot it and expect to see a Gaussian shape, which will look better and more like a real Gaussian as you include more and more y values. It sort of looks like maybe you had a hint of that when you calculated "bins", but never followed through with calling hist() or histc().
Plus, you're not even doing anything with the coefficients you get back from polyfit().
By the way, is this homework?
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!