\% Newton Method to Solve f(x)=x*x - 4 Sin(x)=0,
\% Date: October 16,2002
\% by Yuri V Lvov
format long;
tolerance=.001; \%tolerance to the result
pr = 3; next=0;
while ((abs(pr*pr - 4 * sin(pr))>tolerance)),
next=(pr-((pr*pr - 4 * sin(pr))/(2*pr-4*cos(pr))));
\% This is x[{k+1}]=x[k]-f(x)/f'(x)
pr=next;
x=[next, next^ 2-4*sin(next)]
end
\end{vervatun}
\subsection{Secant}
\% Secant Method to Solve f(x)=0, for user supplied f(x)
\% Date: October 16,2002
\% by Yuri V Lvov
format long;
x=[];x(1)=1;x(2)=2;
tolerance=.001; %tolerance to the result
i=3;
while (abs(x(i-1)-x(i-2))>tolerance),
x(i)=(x(i-1)- f(x(i-1))*(x(i-1)-x(i-2))/(f(x(i-1))-f(x(i-2))));
disp(i);disp(x(i));disp(' ');
i=i+1;
end
\subsection{Exploring Interpolation}
\begin{verbatim}
\% Interpolation examples
\% adapted by Yuri Lvov
\% Choose Number of points to play with:
Number=10;
t=linspace(.2,2.3,Number)
y=1./t+t.$\hat{\ }$2
a=polyfit(t,y,Number-1)
x=linspace(.2,2.3,100);
f=1./x+x.$\hat{\ }$ 2;
ftilde=polyval(a,x);
plot(x,f,'r',x,ftilde,'g',t,y,'mo')
\% Shows Function, interpolation and dots on the same plot
\% Try unequal spaced points, result is strange
t=[.2 .3 .5 .6 1.6 2.3]
y=1./t+t.$\hat{\ }$ 2
a=polyfit(t,y,Number-1)
x=linspace(.2,2.3,100);
f=1./x+x.$\hat{\ }$ 2;
ftilde=polyval(a,x);
plot(x,f,'r',x,ftilde,'g',t,y,'mo')
Number=20;
\% This is the classical example producing wiggles
t=linspace(-1,1,Number);
\% t=-cos((2*(1:Number)-1)*pi./(2*Number));
\% Uncomment the preveous line for C. points
y=1./ (1+25* t.$\hat{\ }$ 2);
a=polyfit(t,y,Number-1);
x=linspace(-1,1,200);
f=1./(1+25*x.$\hat{\ }$ 2);
ftilde=polyval(a,x);
plot(x,f,'r',x,ftilde,'g',t,y,'mo')
\% Do the same with SPLINE interpolation
\% Type 'help spline' to get more info
\% type 'help interp1' to get even more information
x = -1:.1:1;
y = 1./(1+25*x.$\hat{\ }$ 2);
xx = -1:.01:1;
yy = spline(x,y,xx);
plot(x,y,'o',xx,yy)