• Non ci sono risultati.

ˆ Solution for Ex 1. At rst write the following function.

N/A
N/A
Protected

Academic year: 2021

Condividi "ˆ Solution for Ex 1. At rst write the following function."

Copied!
4
0
0

Testo completo

(1)

ˆ Solution for Ex 1. At rst write the following function.

1

function [yt,b]=NewtonInterp(x,y,xt)

2

% Newton form of the interpolating polynomial

3

n=length(x); b=y;

4

for i=2:n

5

for j=2:i

6

add=eps*((x(i)

x(j

1))<1.e

5);

7

b(i)=(b(i)

b(j

1))/(x(i)

x(j

1)+add);

8

end

9

end

10

% part2: Horner scheme for evaluating the polynomial

11

for i=1:length(xt)

12

yt(i)=b(n);

13

for k=n

1:

1:1

14

yt(i)=yt(i)*(xt(i)

x(k))+b(k);

15

end

16

end

ˆ Then, on the script Esercizio1:

1

clear all

2

close all

3

clc

4

f=@(x) x+exp(x)+20./(1+x.^2)

5; n=12;

5

x=linspace(

2,2,n); % interpolation nodes

6

y=f(x); xt=linspace(

2,2); % function values

7

p=NewtonInterp(x,y,xt);

8

err1=max(abs((p

f(xt))))

9

figure(), plot(x,y,'or',xt,p,'

−−

r',xt,f(xt),'k');

10

legend('Equispaced points','Newt. interp.',...

11

'Original function');

1

(2)

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 -5

0 5 10 15 20

Equispaced points Newt. interp.

Original function

2

(3)

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

clear all

2

close all

3

a=0; b=1;

4

h =0.05; x=a:h:b; % equispaced points

5

y=0.1+x+(10^(

1))*rand(size(x)); % the data

6

xx=linspace(a,b); % for the evaluation

7

P=polyfit(x,y,1); % construct the approximant

8

Y = polyval(P,xx); % evaluate the approximant

9

plot(x,y,'or',xx,Y,'

') % plot

10

legend('Data',...

11

'Approximating polynomial')

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

0 0.2 0.4 0.6 0.8 1 1.2

Data

Approximating polynomial

3

(4)

ˆ Solution for Ex 3. Modify the given script as follows.

1

clear all

2

close all

3

a=0; b=2*pi;

4

h=0.01; x=a:h:b; % nodes

5

y=cos(2*x)+(10^(

1))*rand(size(x)); % perturbed data

6

kk = 0;

7

while kk <= 14

8

n = kk;

9

% computes the coefficients

10

coeff=polyfit(x,y,n);

11

% evaluate the coefficients

12

z=polyval(coeff,x);

13

% computes the residual

14

err2=norm(z

y,2);

15

fprintf('\n \t n: %2.0f norma2: %1.2e',n,err2);

16

% plot the results

17

ht=1/10000; u=a:ht:b;

18

v=polyval(coeff,u);

19

plot(x,y,'r.'); hold on;

20

plot(u,v,'k

','LineWidth',2);

21

titlestr=strcat('Minimi quadrati di grado:',...

22

num2str(n));

23

title(titlestr);

24

legend('Dati','Approssimante Minimi quadrati');

25

hold off;

26

pause(.2);

27

kk = kk+1;

28

end

29

fprintf('\n \n');

4

Riferimenti

Documenti correlati

Karl Oelschäger identi…ed this intermediate problem, between mean …eld and contact interactions, called intermediate (or moderate) regime, where it is possible to prove that S t

Whitt, Martingale proofs of many-server heavy-tra¢ c limits for Markovian queues, Probability

[r]

In practice we need to compare the number of iterations needed by each algorithm to reach the solution up to the desired tolerance... First verify (on paper) that the xed point

[r]

Thus we can have solutions with fast decay, corresponding to trajectory converging to the origin, and with slow decay, which are the ones described in the thesis.. Note that this

Write a function that takes a string, and reorders all the letters in alphabetic order... The function contains

*8) Chemical engineers practice a profession and must obey rules governing their professional conduct. One important set of rules that all engineers should be aware of is