Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %ex1
- %define variables
- syms a b c
- %define data
- x=[1,2,3,4];
- y=[7,6,4,1];
- %define your function
- f=0;
- for i=1:length(x)
- f=f+((a*x(i)^2+b*x(i) +c)-y(i))^2;
- end
- %calculate derivatives
- da=diff(f,a);
- db=diff(f,b);
- dc=diff(f,c);
- %solve the equations
- sol=solve([da==0,db==0,dc==0],[a,b,c]);
- %plot the solution
- x1=0:0.1:5;
- y1=sol.a*x1.^2+sol.b*x1+sol.c
- figure(1)
- plot(x1,y1,'r');
- hold all;
- for k=1:length(x);
- figure(1)
- plot(x(k),y(k),'bo');
- end
- hold off; grid on;
- xlabel('X'), ylabel('Y'),
- title('Linear approximation')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement