Issue Finding the Mean Squared Error / Polyfit MATLAB

In summary, the conversation involves discussing a homework problem where the individual is struggling with their code and trying to calculate fit errors. They also mention using polyfit and log functions in MATLAB. The expert suggests using vector operations instead of for loops and avoiding hard coding numbers.
  • #1
SchrodingersMu
14
0

Homework Statement



upload_2015-3-12_22-21-7.png

upload_2015-3-12_22-21-45.png

Homework Equations


Given above

The Attempt at a Solution


I used polyfit, but my mean swuare errors are way bigger than they should be- don't see what is wrong with my code! My code is ugly btw, my apologies.

%Hw 7
clear all
close all

y3=[1960;
1965;
1970;
1975;
1980;
1985;
1990;
1995];

y=[0;5;10;15;20;25;30;35];w=[26.4;
31.9;
38.6;
46.7;
56.5;
68.3;
82.7;
100.0];

p=polyfit(y,w,1);

%Compute in 5 years and 10 years
k=2000;

future=((k-1995)+35);
Eq=2.0669*future+20.2167;

%After 40 years
Eq1=2.0669*40+20.2167;

%After 45 years
Eq2=2.0669*45+20.2167;

save w_2000_deg1.dat Eq1 -ascii
save w_2005_deg1.dat Eq2 -ascii

%Find error
y1=[0;5;10;15;20;25;30;35;40;45];
y2=y1+1;
w1=[26.4;31.9;38.6;46.7;56.5;68.3;82.7;100.0;102.8927;113.2272];

n=10;
c=1;
e=0;
for i=1:n
e(i)=((2.066*y1(i,c)+20.2167)-w1(i,c))^2;
end
r=sum(e);
e_ans=(1/10)*r;

save e_deg1.dat e_ans -ascii

%Part b
s=log(w);

y4=y3-1959;
pp=polyfit(y4,s,1);

%model=0.0381*t+3.2347;
%c=e^alpha a=beta
g1=exp(3.2728)*exp(0.0381*40);
g2=exp(3.2728)*exp(0.0381*45);save w_2000_exp.dat g1 -ascii
save w_2005_exp.dat g2 -ascii

%Find error

n=10;
c=1;
h=0;
for i=1:n
h(i)=((exp(3.2728)*exp(0.0381*y1(i,c)))-w1(i,c))^2;
end
r=sum(h);
h_ans=(1/10)*r;

save e_exp.dat h_ans -ascii
Everything else is fine, but MATLAB gave e_ans= 15.975448337000001
and h_ans=1.442099806906659e+02 . should be way smaller

Any help is appreciated!
 
Physics news on Phys.org
  • #2
You can't calculate a fit error for a data point you don't have, so you should calculate a sum of 8 points, not 10. Your actual error should be bigger. Why do you think it should be smaller? Have you checked the actual numbers?

By the way, you shouldn't have hard coded numbers like 2.0669, use p(1) instead. You should also avoid for loops in Matlab and use vector operations.
 
  • Like
Likes SchrodingersMu

Related to Issue Finding the Mean Squared Error / Polyfit MATLAB

1. What is the mean squared error (MSE) in the context of polyfit in MATLAB?

The mean squared error (MSE) is a measure of how well the data points fit a chosen polynomial function in MATLAB. It is the average of the squared differences between the actual data points and the predicted values from the polynomial function. A lower MSE indicates a better fit of the function to the data.

2. How do I calculate the mean squared error in MATLAB?

In MATLAB, the mean squared error can be calculated using the 'polyfit' function, which takes in the data points and the desired degree of the polynomial function as inputs. The 'polyval' function can then be used to calculate the predicted values from the polynomial function. Finally, the 'mse' function can be used to calculate the mean squared error between the actual data and the predicted values.

3. What is the significance of MSE in data analysis?

The mean squared error is a widely used metric in data analysis as it helps to evaluate the accuracy of a chosen model or function in fitting the data. A lower MSE indicates a better fit and therefore, a more accurate representation of the data. This allows for the comparison of different models and choosing the one with the lowest MSE as the best fit.

4. Can the mean squared error be used to determine the best degree of a polynomial function in MATLAB?

Yes, the mean squared error can be used to determine the best degree of a polynomial function in MATLAB. By trying different degrees and calculating the MSE for each, one can choose the degree that results in the lowest MSE as the best fit for the data. However, this should be done with caution as a higher degree polynomial may lead to overfitting and a lower MSE on the training data, but may not perform well on new data.

5. Is it possible to have a negative mean squared error in MATLAB?

No, it is not possible to have a negative mean squared error in MATLAB as it is calculated by squaring the differences between the actual data and the predicted values. This ensures that the MSE is always a positive value. A negative MSE would indicate that the predicted values are better than the actual data, which is not possible.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
Back
Top