How to calculate mean square error

5 views (last 30 days)
Adriana
Adriana on 13 Mar 2015
Edited: Guillaume on 13 Mar 2015
I am supposed to find the mean square error of a least square fit that I did. I cannot find where the bug in my code is. My error is huge- should be ~ 1e-4
%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];
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
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
Any input is appreciated!!

Answers (0)

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!