Solution for Ex 0. Write the following function.
1 function [x,iter] = NewtonMod(f,fp,x0,tol,maxiter,m) 2 %NEWTON for finding multiple zeros of functions 3 res = tol+1; iter = 0; % Initialize
4 while res > tol && iter < maxiter 5 x0 = x0 − m*f(x0)/fp(x0);
6 res = abs(f(x0)); iter = iter+1;
7 end 8 x = x0;
9 if iter >= maxiter
10 disp('no convergence iter > maxiter') 11 end
1
Solution for Ex 1. Write the following function.
1 function [x,iter] = fixedpoint(g,x0,tol,maxiter)
2 %FIXEDPOINT for finding fixed−points of the function g 3 res = tol+1; iter = 0; % Initialize
4 while res > tol && iter < maxiter
5 x1 = g(x0);
6 res = abs(x1−x0); x0 = x1; iter = iter+1;
7 end 8 x = x1;
9 if iter >= maxiter
10 disp('no convergence iter > maxiter') 11 end
2
Solution for Ex 2. On the script called Esercizio2 write 1 close all
2 clear all 3 clc
4 f = @(x) x.^2− log(x.^2 + 2);
5 x = linspace(−2,2);
6 figure 7 hold on
8 plot(x,f(x)), grid;
9 %% g = +(−) sqrt(log(x.^2 + 2))
10 gp = @(x) x./sqrt(log(x.^2+2))./(x.^2+2);
11 figure 12 hold on
13 plot(x,gp(x)), grid;
14 %%
15 g = @(x) sqrt(log(x.^2 + 2)); format long 16 x1=fixedpoint(g,2,1.e−06,50)
17 x2=fzero(f,2) 18 %%
19 g = @(x) −sqrt(log(x.^2 + 2)); format long 20 x1=fixedpoint(g,−2,1.e−06,50)
21 x2=fzero(f,−2)
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
-1 -0.5 0 0.5 1 1.5 2 2.5
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
-0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4
3