Coefficients from the highest order to the lowest

Info icon This preview shows pages 3–8. Sign up to view the full content.

View Full Document Right Arrow Icon
%coefficients from the highest order to the lowest c1p=polyfit(x,y,d1) c2p=polyfit(x,y,d2) c1 c2 c2 = 10.0355 1.0639 0.6928 c1p = 8.6850 -6.2364 c2p = 0.6928 1.0639 10.0355 c1 = -6.2364 8.6850 c2 = 10.0355 1.0639 0.6928
Image of page 3

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

View Full Document Right Arrow Icon
Problem 4 %the more sophisticated way clf regr= load( 'regression.txt' ); x=regr(:,1); y=regr(:,2); f = cell(1,3); f{1} = @( xx )( sin(xx.^3) ); f{2} = @( xx )( xx.^2 ); f{3} = @( xx )( cos(xx)./xx ); p=my_ls_2(x,y,f) %Note to grader: this is my_ls2(x,y) is %using a more specific function that does not %input a basis function p2=my_ls2(x,y) %note that only one way was required to get %full credit y_reg=p(1)*sin(x.^3) + p(2)*x.^2 + p(3)*cos(x)./x; plot(x,y, '.' ,x,y_reg, 'LineWidth' ,4) legend( 'given' , 'regressed' ) type my_ls_2 type my_ls2
Image of page 4
p = 2.9795 -2.0416 7.9555 p2 = 2.9795 -2.0416 7.9555 function [p] = my_ls_2(x1,y1,fun) sx=size(x1); sy=size(y1); if sx(1)==1 x_v=x1'; %convert row vector into column vector else x_v=x1; end if sy(1)==1 y_v=y1'; %convert row vector into column vector else y_v=y1; end A=zeros(length(x_v),length(fun)); for i=1:length(x_v); for j=1:length(fun); A(i,j)=fun{j}(x_v(i)); end end p=A\y_v; function [coefficients] = my_ls2(x1,y1) sx=size(x1); sy=size(y1); if sx(1)==1 x_v=x1'; %convert row vector into column vector else x_v=x1; end if sy(1)==1 y_v=y1'; %convert row vector into column vector
Image of page 5

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

View Full Document Right Arrow Icon
else y_v=y1; end X=[]; X(:,1)= sin(x_v.^3); X(:,2)= x_v.^2; X(:,3)=cos(x_v)./x_v; a=X\y_v; coefficients=a; %gives coefficients in order from... %p1*f1(x) + p2*f2(x)...pn*f2(x) Problem 5 clf %part a %ln(y)= a + bx + cx^2 + dx^3 %part b reg=load( 'exp_mat.txt' ); x1=reg(:,1); y1=reg(:,2);
Image of page 6
coeff=my_ls(x1,log(y1),3); a=round(coeff(1))
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern