homework2 juan pablo madrigal cianci
TRANSCRIPT
-
8/13/2019 Homework2 Juan Pablo Madrigal Cianci
1/4
Juan Pablo Madrigal Cianci
Homework set 2.
1. The following code calculates the root of a function using the bisection method (given aninterval and a tolerance). It was also used on the next exercise.
functionbis=biseccion(f,a,b,tol)format long
%description
display ('**********************************************')display ('* *')display ('*THIS PROGRAM RETURNS THE ROOT OF 2X-1=SIN(X)*')display ('* *')display ('**********************************************')s=0;
%decides if there's a 0 or not.iff(a)*f(b)>0
display('there are no zeros on the selected interval. Try again')endfa=f(a);fb=f(b);
%sequencewhile(b-a)/2 >tol
c=(a+b)/2;fc=f(c);s=s+1;iffc==0
display ('the answer is')disp(c)
endiffc*fa
-
8/13/2019 Homework2 Juan Pablo Madrigal Cianci
2/4
2. Given 2x-1=sin(x)
a) () = 0can be written in the form: 2 1 sin()= 0.
b) to find the interval, we first notice that sin(x) takes values between 1 and -1, so, when 2x-1 is bigger
than 1, the function will always be positive. That is, x>1. When the function is evaluated at 0, it gives
a negative number, which implies that theres a change of sign on f between [0,1] and therefore,
theres a root in that interval
c) 2x-1-sin(x) is an injective & increasing function so it will only have one root in the aforementioned
interval. Also, because x is sufficiently small to be compared to sin(x) (for small values, sin(x) is really
close to x) the only way that the function can possibly be 0 is in this interval.
d) The code is the same as in exercise 1. The output is:
3). The 2 plots and the code are attached.
b) Itsbecause when the derivative of the functions (sin(x) for both) is evaluated in x*, its value isgoing
to be less than 1.
c) They have different fixed points. It takes a little more time for the case of cos(x)+2 to converge
because the slope at fixed point ( x**) (intersection with the line y=x) is bigger that the slope at the
fixed point( x* )for cos(x).
d) The iterations should converge everywhere. The only way they wouldnt be globally convergent
would be if their fixed point was located at =
, (it would make their derivative equal to one) butthis is clearly not the case.
4. As it can be seen in the plot, the function () = 4 3has 5 roots.
-
8/13/2019 Homework2 Juan Pablo Madrigal Cianci
3/4
To find the roots using the FPI method, the following code was used:
functiony=fpi2(g,x0,tol,kmax)%uses a function, a tolerance, an initial value
and a maximum number of iterations
%descriptiondisplay ('***********************************************')display ('* *')display ('*THIS PROGRAM USES THE FPI METHOD. *')display ('* *')display ('* jpmadrigalc *')display ('***********************************************')
%actual codefork=1:kmaxx = g(x0);relerr = abs(x-x0)/( abs(x) );%relative error
if relerr
-
8/13/2019 Homework2 Juan Pablo Madrigal Cianci
4/4
The other 3 roots didnt converge because the absolute value of the derivative of the function
(1 4())evaluated in that point is bigger than 1. i.e.:
|1 4( )| > 1