From: Nina Hinchy on 11 Feb 2010 13:06 Hi, I've just started using matlab and in order to gain experience I've been coding up some programs to perform simple tasks. To learn how to manipulate polynomials, I created a program to calculate and plot the normal to a curve at a given point. But it doesn't work. The resulting line isn't the normal at the point that I specify. Here's my code, if anybody has any ideas, I would greatly appreciate it! Apologies for my somewhat trivial question but you have to start somewhere! % - Define a polynomial p = [1 1 2 3]; % - Find a point on the curve x1 = 5; y1 = polyval(p,x1); % - Get the derivative of the polynomial px = polyder(p); % - get the slope of the tangent at point x1 t_slope = polyval(px, x1); n_slope = -1./t_slope; %Equation of tangent = y - y1 = t_slope(x -x1) % y = t_slope*x -t_slope*x1 + y1 % y = dx + e d = t_slope; e = -t_slope*x1 + y1; t_eq = [d e]; %equation of normal = y - y1 = n_slope(x -x1) % y = n_slope*x -n_slope*x1 + y1 % y = ax + c a = n_slope; c = -n_slope.*x1 + y1; n_eq = [a c]; % - Define range x = -2:1:10; % - Plot the polynomial on given range y = polyval(p,x); plot(x,y, x1,y1, 'o'); hold on; n_pts = polyval(n_eq,x); t_pts = polyval(t_eq,x); plot(x, n_pts, x, t_pts); hold off; nina
From: John D'Errico on 11 Feb 2010 13:44 "Nina Hinchy" <n.hinchy1(a)gmail.com> wrote in message <hl1guc$oj2$1(a)fred.mathworks.com>... > Hi, > > I've just started using matlab and in order to gain experience I've been coding up some programs to perform simple tasks. To learn how to manipulate polynomials, I created a program to calculate and plot the normal to a curve at a given point. But it doesn't work. The resulting line isn't the normal at the point that I specify. > Here's my code, if anybody has any ideas, I would greatly appreciate it! > Apologies for my somewhat trivial question but you have to start somewhere! > > > % - Define a polynomial > p = [1 1 2 3]; > > % - Find a point on the curve > x1 = 5; > y1 = polyval(p,x1); > > % - Get the derivative of the polynomial > px = polyder(p); > > % - get the slope of the tangent at point x1 > > t_slope = polyval(px, x1); > n_slope = -1./t_slope; > > > %Equation of tangent = y - y1 = t_slope(x -x1) > % y = t_slope*x -t_slope*x1 + y1 > % y = dx + e > > d = t_slope; > e = -t_slope*x1 + y1; > t_eq = [d e]; > > > > %equation of normal = y - y1 = n_slope(x -x1) > % y = n_slope*x -n_slope*x1 + y1 > % y = ax + c > > a = n_slope; > c = -n_slope.*x1 + y1; > n_eq = [a c]; > > > > % - Define range > x = -2:1:10; > > % - Plot the polynomial on given range > y = polyval(p,x); > plot(x,y, x1,y1, 'o'); > hold on; > n_pts = polyval(n_eq,x); > t_pts = polyval(t_eq,x); > plot(x, n_pts, x, t_pts); > hold off; > > nina It IS normal. You have not looked at the axes carefully enough. Change the coefficients of the polynomial p slightly, so that the axes are scaled roughly the same. So, p = [1 1 2 3]/100; Then, at the VERY end, add ONE more line. axis equal This forces the axes to be identical. Now look at your plots. While it is good that you plotted what you were doing, you needed to look carefully at the result to understand what you did. John
From: Cygnine on 11 Feb 2010 13:51 "Nina Hinchy" <n.hinchy1(a)gmail.com> wrote in message <hl1guc$oj2$1(a)fred.mathworks.com>... > Hi, > > I've just started using matlab and in order to gain experience I've been coding up some programs to perform simple tasks. To learn how to manipulate polynomials, I created a program to calculate and plot the normal to a curve at a given point. But it doesn't work. The resulting line isn't the normal at the point that I specify. Your code and results look fine. Try adding the following after your plots: axis([4 6 162 164]); The `problem' is that you just chose a really bad example point. Try a point whose tangent slope isn't so large. (E.g. change x1 to 0)
From: Nina Hinchy on 12 Feb 2010 04:47 "Cygnine " <cygnine(a)remove.this.gmail.com> wrote in message <hl1jio$i1u$1(a)fred.mathworks.com>... > "Nina Hinchy" <n.hinchy1(a)gmail.com> wrote in message <hl1guc$oj2$1(a)fred.mathworks.com>... > > Hi, > > > > I've just started using matlab and in order to gain experience I've been coding up some programs to perform simple tasks. To learn how to manipulate polynomials, I created a program to calculate and plot the normal to a curve at a given point. But it doesn't work. The resulting line isn't the normal at the point that I specify. > > Your code and results look fine. Try adding the following after your plots: > > axis([4 6 162 164]); > > The `problem' is that you just chose a really bad example point. Try a point whose tangent slope isn't so large. (E.g. change x1 to 0) Thanks for your help! Nina
|
Pages: 1 Prev: Need Help with Curve Fit Parameter Error Next: reshape table to matrix while grouping rows |