Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. %ex1
  2.  
  3. %define variables
  4. syms a b c
  5. %define data
  6. x=[1,2,3,4];
  7. y=[7,6,4,1];
  8. %define your function
  9. f=0;
  10. for i=1:length(x)
  11. f=f+((a*x(i)^2+b*x(i) +c)-y(i))^2;
  12. end
  13. %calculate derivatives
  14. da=diff(f,a);
  15. db=diff(f,b);
  16. dc=diff(f,c);
  17. %solve the equations
  18. sol=solve([da==0,db==0,dc==0],[a,b,c]);
  19. %plot the solution
  20. x1=0:0.1:5;
  21. y1=sol.a*x1.^2+sol.b*x1+sol.c
  22. figure(1)
  23. plot(x1,y1,'r');
  24. hold all;
  25. for k=1:length(x);
  26. figure(1)
  27. plot(x(k),y(k),'bo');
  28. end
  29. hold off; grid on;
  30. xlabel('X'), ylabel('Y'),
  31. title('Linear approximation')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement