From CSE100B Wiki
%GOAL: The goal of this program is to solve for the roots of a parabola
%using Newton's Method. The results will be the same as the
%Quadratic Formula, but will use iterations rather than a formula.
% Learn about Newton's Method: http://en.wikipedia.org/wiki/Newton's_method
%PRACTICAL APPLICATION: This program is significant because your graphing calculator uses
%iterative formulas and power series to solve many problems you input. This
%specific function essentially replicates the "Solve for X Intercept"
%function on a TI-83 calculator for the case of second degree polynomials.
%METHOD: The program takes the coeffiecients of a parabola,
%ax^2+bx^1+cx^0=f(x), and an initial guess of an x coordinate of n based on the extrema to yield
%the x coordinate of the function. Accuracy is achieved by successive iterations.
%First, it checks for the number of intercepts via a determinent.
%This function uses a while loop to find out when the answer is
%GOING FURTHER: This method can be applied to replicating trigonometric
%functions using power series (Your calculator doesn't have sine of .2
%programmed in - it plugs it into a power series!). It can be used to
%calculate integrals (and to compare the relative accuracies of LRAM,
%RRAM,MRAM, Trapezoid method - methods of approximating a definite integral. Google for details).
% You can even try calculating Pi by approximating
%the integral of a unit circle or by squaring the gamma function of one
%Ok, finally time for the function:
disp('Input real constants from above form:')
a=input('a = ');
b=input('b = ');
c=input('c = ');
disp('The function you entered is a line with the intercept at')
disp(['x = ' num2str(-c/b) ])
d=b*b-4*a*c; %d is the determinant, which is used to calculate the number of intercepts the parabola has.
e=-b/(2*a); %This defines e as the x coordinate of the extremum
if d<0 %No real roots case
disp('No real x-axis intercepts exist for the function.')
elseif d==0 %One real root case
disp('One real x-axis intercept occurs at ')
disp(['x = ' num2str(e) ]) %The extremum must be the intercept if only one intercept exists. No approximations necessary.
elseif d>0 %Two real roots case - the real fun starts here
disp('Two real x-axis intercepts exist at ')
guess=[ e-1, e+1]; %Sets the initial 'guesses' of the intercepts on both sides of the extrema
while (n(i)<=1000)||(abs(nold-nnew)>=(10^(-10)))%Defining our precision
nnew=nold-(a*nold*nold+b*nold+c)/(2*a*nold+b); %This term is the core of the program- it is the iteration term for Newton's method.
n(i)=n(i)+1; %For fun / debugging, tracking how many iterations it takes
disp([ 'x = ' num2str(answer(1)) ])
disp([ 'x = ' num2str(answer(2)) ])
h=a*e*e+b*e+c; %This defines the y-value of the extremum
disp('Furthermore . . . ')
disp('The extremum occurs at ')
disp([ 'x = ' num2str(e) ])
disp([ 'y = ' num2str(h) ])
if a>0 %Second derivative test; 2 term in front of a omitted for simplicity.
disp('This extrememum is a MINIMUM.')
elseif a<0 %Second derivative test; 2 term in front of a omitted for simplicity.
disp('This extremum is a MAXIMUM.')
error('Conditions not met')