• Non ci sono risultati.

1. Scrivere una funzione di Matlab per approssimare Z b

N/A
N/A
Protected

Academic year: 2021

Condividi "1. Scrivere una funzione di Matlab per approssimare Z b"

Copied!
8
0
0

Testo completo

(1)

Esercizi

1. Scrivere una funzione di Matlab per approssimare Z b

a

exp(−x 2 ) dx

usando la formula dei trapezi con N sottointervalli:

Z

b

a

f (x ) dx ≈ H 2

"

f (x

0

) + 2

N−1

X

i =1

f (x

i

) + f (x

N

)

#

dove H = (b − a)/N e x i = a + iH per i = 0, . . . , N.

2. Scrivere una funzione di Matlab per approssimare Z b

a

f (x ) dx

usando la formula dei trapezi con N sottointervalli.

(2)

Trapezi

function sol=trapezi(fun,a,b,N) h=(b-a)/N;

x=[a:h:b];

fx=feval(fun,x);

sol=h*(fx(1)/2+sum(fx(2:N))+fx(N+1)/2);

return

(3)

Altre formule di quadratura composite

I Formula del punto medio con N sottointervalli:

Z b a

f (x ) dx ≈ H

N

X

i =1

f  x i −1 + x i 2

 .

I Formula di Cavalieri-Simpson con N sottointervalli:

Z b a

f (x ) dx ≈ H 6

N

X

i =1



f (x i −1 ) + 4f  x i −1 + x i 2



+ f (x i )

 .

H = (b − a)/N, x i = a + iH, i = 0, 1, . . . , N.

(4)

Esercizio

Scrivere una funzione di Matlab per approssimare Z b

a

f (x ) dx usando

I la formula del punto medio con N sottointervalli;

I la formula di Cavalieri-Simpson con N sottointervalli.

(5)

Punto medio e Simpson

function risultato=PM(f,a,b,N) H=(b-a)/N;

x=[a+H/2:H:b-H/2];

fx=feval(f,x);

risultato=H*sum(fx);

function sol=simpson(fun,a,b,N) h=(b-a)/N;

x=[a:h/2:b];

fx=feval(fun,x);

sol=h/6*(fx(1)+4*sum(fx(2:2:end-1))+2*sum(fx(3:2:end-2))+fx(end));

return

(6)

Il comando quad

>> Q = quad(FUN,A,B)

“Tries to approximate the integral of scalar-valued function FUN

from A to B to within an error of 1.e-6 using recursive adaptive

Simpson quadrature. FUN is a function handle. The function

Y=FUN(X) should accept a vector argument X and return a vector

result Y, the integrand evaluated at each element of X.”

(7)

Esercizi

1. Scrivere una funzione di Matlab per approssimare Z b

a

f (x ) dx

con errore stimato minore di toll usando la formula di Simpson.

function [sol,N,flag]=simpsontoll(fun,a,b,toll) N=1;

solold=simpson(fun,a,b,N);

flag=1;

for k=1:100 N=2*N;

sol=simpson(fun,a,b,N);

err=abs(sol-solold)/15;

if err < toll, flag=0; return, end solold=sol;

end

(8)

Esercizi

2. Scrivere una funzione di Matlab che data una funzione f , un intervallo [a, b] e un numero naturale N calcoli:

I

il polinomio P

N

che interpola f nei N + 1 nodi di Chebyshev dell’intervallo [a, b];

I

l’integrale fra a e b di P

N

.

function [P,integrale]=quadcheb(f,a,b,N) xch=-cos(pi*[0:N]/N);

xch=(a+b)/2+(b-a)/2*xch;

fxch=feval(f,xch);

P=polyfit(xch,fxch,N);

Q=polyint(P);

integrale=polyval(Q,b)-polyval(Q,a);

Riferimenti

Documenti correlati

Abstract Weighted labelled transition systems (WLTSs) are an estab- lished (meta-)model aiming to provide general results and tools for a wide range of systems such

Figure 22: A cytoplasmic mutant p53 drives the oncogenic response to insulin. A-B) Insulin-induced proliferation and invasion are mediated by cytoplasmic mutant p53.

Scrivere la funzione Matlab find_val che riceva in input un vettore v e una variabile stringa tipo che puo' assumere solo i valori 'min' e 'max' e restituisca in output il

“Tries to approximate the integral of scalar-valued function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. FUN is a

Nel caso teorico con aritmetica esatta, quante iterazioni deve eseguire l’algoritmo al massimo per risolvere

tendons: it was reported that the production of tenascin-C and collagen XII, closely associated to type I collagen fibrils[127], is high in fibroblasts attached to a stretched

Horowitz and Malkhi [9]propose a scheme for dynamically estimating net- work size at each node of the network as the network evolves (that is, nodes join and leave the network),

analogously BB ′ and CC ′ are the bisectors of the angles ∠ABC