• Non ci sono risultati.

ˆ Solution for Ex 1. Write the following functions.

N/A
N/A
Protected

Academic year: 2021

Condividi "ˆ Solution for Ex 1. Write the following functions."

Copied!
2
0
0

Testo completo

(1)

ˆ Solution for Ex 1. Write the following functions.

1

function [x,iter] = bisection(f,a,b,tol,maxiter)

2

%BISECTION for finding zeros of functions

3

res = tol+1; iter = 0; % Initialize

4

while res > tol && iter < maxiter

5

c = (a+b)/2;

6

if f(c) == 0

7

x = c; break;

8

elseif f(c)*f(a) < 0

9

b = c;

10

elseif f(c)*f(b) < 0

11

a = c;

12

end

13

res = abs(f(c)); iter = iter+1;

14

end

15

x = c;

16

if iter >= maxiter

17

disp('no convergence iter > maxiter')

18

end

1

function [x,iter] = newton(f,fp,x0,tol,maxiter)

2

%NEWTON for finding zeros of functions

3

res = tol+1; iter = 0; % Initialize

4

while res > tol && iter < maxiter

5

x0 = x0

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

(2)

ˆ Solution for Ex 2. On the script named Esercizio2 write the following code.

1

clear all

2

close all

3

clc

4

format long

5

f = @(x) exp(x)

4*x.^2;

6

x = linspace(

2,5);

7

figure

8

hold on

9

grid on

10

plot(x,f(x))

-2 -1 0 1 2 3 4 5

-20 -10 0 10 20 30 40 50

1

fp = @(x) exp(x)

8*x;

2

x1 = bisection(f,

1,0,1.e

06,50)

3

x2 = newton(f,fp,1,1.e

06,50)

4

x1 =

0.407776832580566

5

x2 = 0.714805912364580

2

Riferimenti

Documenti correlati

In Section 4 we prove Theorem 1.1 by combining the Pohozaev type identity with the results of our local blow-up analysis.. Section 5 is devoted to the computation of the

[r]

[r]

sapply: Same as lapply but try to simplify the result apply: Apply a function over the margins of an array tapply: Apply a function over subsets of a vector mapply: Multivariate

ACF.gls: Autocorrelation Function for gls Residuals (nlme) anova.gls: Compare Likelihoods of Fitted Objects (nlme) gls: Fit Linear Model Using Generalized Least Squares (nlme)

start(): extracts and encodes the times the first observation were taken (stats) time(): creates the vector of times at which a time series was sampled (stats) ts():

The definition is the same as that for an ordinary function, and is best explained using limits:. Let a ∈

The mathematical representations characterized by the smallest number of nonzero parameters are called canonical forms.. • The most interesting canonical forms are