hw9_sol - ASE 211 Homerwork 9 - Solution %This program...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ASE 211 Homerwork 9 - Solution %This program constructs the following least-squares %approximants which fit the data of Problem P8.29 (p.277) %and plots the functions versus the given data. % % 1.linear % 2.quadratic % 3.exponential % x=linspace(0,10,11); y=[1,1,0.5556,0.3571,0.2615,0.2063,0.1705,0.1453,0.1267,0.1123,0.1]; n=length(x); sumx=0 sumy=0 sumxy=0 sumx2=0 sumy2=0; sumx2y=0; sumx4=0; sumx3=0; sumlny=0; sumxlny=0; for i=1:n sumx=sumx+x(i); sumy=sumy+y(i); sumxy=sumxy+x(i)*y(i) ; sumx2=sumx2+x(i)^2; sumy2=sumy2+y(i)^2; sumx2y=sumx2y+x(i)^2*y(i); sumx3=sumx3+x(i)^3; sumx4=sumx4+x(i)^4; sumlny =sumlny+log(y(i)) sumxlny=sumxlny+x(i)*log(y(i)) ; end %linear fit %========== A_linear=[n sumx;sumx sumx2] b_linear=[sumy;sumxy] a_linear=A_linear\b_linear %quadratic fit %============= A_quad=[n sumx sumx2;sumx sumx2 sumx3;sumx2 sumx3 sumx4] b_quad=[sumy; sumxy; sumx2y] a_quad=A_quad\b_quad %exponential fit %=============== A_exp=[n sumx;sumx sumx2] b_exp=[sumlny;sumxlny] a_exp=A_exp\b_exp; a_exp(1)=exp(a_exp(1)) %plotting %======== xx=linspace(0,10); y_linear=a_linear(1)+a_linear(2).*xx; y_quad=a_quad(1)+a_quad(2).*xx+a_quad(3).*xx.^2; y_exp=a_exp(1)*exp(a_exp(2).*xx); plot(xx,y_linear,xx,y_quad,'-.',xx,y_exp,'--') legend('linear','quadratic','exponential') hold on plot(x,y,'+') title('Least-squares approximants') xlabel('x') ylabel('y') grid on ...
View Full Document

Page1 / 3

hw9_sol - ASE 211 Homerwork 9 - Solution %This program...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online