Issue Finding the Mean Squared Error / Polyfit MATLAB

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
SchrodingersMu
Messages
14
Reaction score
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!
 
on Phys.org
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   Reactions: SchrodingersMu