You are on page 1of 4

Juan Pablo Madrigal Cianci

Homework set 2.

1.

The following code calculates the root of a function using the bisection method (given an interval and a tolerance). It was also used on the next exercise.

function bis=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. if f(a)*f(b)>0 display('there are no zeros on the selected interval. Try again') end fa=f(a); fb=f(b); %sequence while (b-a)/2 >tol c=(a+b)/2; fc=f(c); s=s+1; if fc==0 display ('the answer is') disp(c) end if fc*fa<0 b=c; fb=fc; else a=c; fa=fc; end end %results display ('the anser is') disp((a+b)/2) display('the number of steps to get to this approximation is') disp(s)

2. Given 2x-1=sin(x) a) () = 0 can 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) Its because when the derivative of the functions (sin(x) for both) is evaluated in x*, its value is going 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) but
2

this is clearly not the case.

4. As it can be seen in the plot, the function () = 4 3 has 5 roots.

To find the roots using the FPI method, the following code was used:
function y=fpi2(g,x0,tol,kmax)%uses a function, a tolerance, an initial value and a maximum number of iterations

%description display ('***********************************************') display ('* *') display ('*THIS PROGRAM USES THE FPI METHOD. *') display ('* *') display ('* jpmadrigalc *') display ('***********************************************')

%actual code for k=1:kmax x = g(x0); relerr = abs(x-x0)/( abs(x) );%relative error if relerr<tol break % it's finished end x0 = x; end if (k==kmax) disp('The algorithm did not converge, try different values') %if this shows up, the values chosen didn't work end y = x;

the code works pretty well, however, the 2 smallest roots and the biggest root couldnt be calculated because the algorithm didnt converge. The other two roots are 3.161826486605397 and -0.544442400675143

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

You might also like