a = [1.2 5.4 6 1.5 9]
b = [5.2 pi 1.2 1.5 2]
a = 1.2000 5.4000 6.0000 1.5000 9.0000 b = 5.2000 3.1416 1.2000 1.5000 2.0000
sqrt(a)
ans = 1.0954 2.3238 2.4495 1.2247 3.0000
e.^(a/2)
ans = 1.8221 14.8797 20.0855 2.1170 90.0171
a + b
ans = 6.4000 8.5416 7.2000 3.0000 11.0000
a.* b
ans = 6.2400 16.9646 7.2000 2.2500 18.0000
v1 = 0 : 0.5 : 30
v1 = Columns 1 through 6: 0.00000 0.50000 1.00000 1.50000 2.00000 2.50000 Columns 7 through 12: 3.00000 3.50000 4.00000 4.50000 5.00000 5.50000 Columns 13 through 18: 6.00000 6.50000 7.00000 7.50000 8.00000 8.50000 Columns 19 through 24: 9.00000 9.50000 10.00000 10.50000 11.00000 11.50000 Columns 25 through 30: 12.00000 12.50000 13.00000 13.50000 14.00000 14.50000 Columns 31 through 36: 15.00000 15.50000 16.00000 16.50000 17.00000 17.50000 Columns 37 through 42: 18.00000 18.50000 19.00000 19.50000 20.00000 20.50000 Columns 43 through 48: 21.00000 21.50000 22.00000 22.50000 23.00000 23.50000 Columns 49 through 54: 24.00000 24.50000 25.00000 25.50000 26.00000 26.50000 Columns 55 through 60: 27.00000 27.50000 28.00000 28.50000 29.00000 29.50000 Column 61: 30.00000
v = linspace(1, 2)
v = Columns 1 through 8: 1.0000 1.0101 1.0202 1.0303 1.0404 1.0505 1.0606 1.0707 Columns 9 through 16: 1.0808 1.0909 1.1010 1.1111 1.1212 1.1313 1.1414 1.1515 Columns 17 through 24: 1.1616 1.1717 1.1818 1.1919 1.2020 1.2121 1.2222 1.2323 Columns 25 through 32: 1.2424 1.2525 1.2626 1.2727 1.2828 1.2929 1.3030 1.3131 Columns 33 through 40: 1.3232 1.3333 1.3434 1.3535 1.3636 1.3737 1.3838 1.3939 Columns 41 through 48: 1.4040 1.4141 1.4242 1.4343 1.4444 1.4545 1.4646 1.4747 Columns 49 through 56: 1.4848 1.4949 1.5051 1.5152 1.5253 1.5354 1.5455 1.5556 Columns 57 through 64: 1.5657 1.5758 1.5859 1.5960 1.6061 1.6162 1.6263 1.6364 Columns 65 through 72: 1.6465 1.6566 1.6667 1.6768 1.6869 1.6970 1.7071 1.7172 Columns 73 through 80: 1.7273 1.7374 1.7475 1.7576 1.7677 1.7778 1.7879 1.7980 Columns 81 through 88: 1.8081 1.8182 1.8283 1.8384 1.8485 1.8586 1.8687 1.8788 Columns 89 through 96: 1.8889 1.8990 1.9091 1.9192 1.9293 1.9394 1.9495 1.9596 Columns 97 through 100: 1.9697 1.9798 1.9899 2.0000
row1 = linspace(10, 20, 6)
row2 = linspace(20, 10, 6)
A = [row1; row2]
row1 = 10 12 14 16 18 20 row2 = 20 18 16 14 12 10 A = 10 12 14 16 18 20 20 18 16 14 12 10
dot(a, b)
ans = 50.655
A = [a; b]
A = 1.2000 5.4000 6.0000 1.5000 9.0000 5.2000 3.1416 1.2000 1.5000 2.0000
y = A.* b
y = 6.2400 16.9646 7.2000 2.2500 18.0000 27.0400 9.8696 1.4400 2.2500 4.0000
M = diag(a)
M1 = diag(a, -1)
M2 = diag(a, 1)
M = Diagonal Matrix 1.2000 0 0 0 0 0 5.4000 0 0 0 0 0 6.0000 0 0 0 0 0 1.5000 0 0 0 0 0 9.0000 M1 = 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.20000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 5.40000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 6.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.50000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 9.00000 0.00000 M2 = 0.00000 1.20000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 5.40000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 6.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.50000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 9.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
col = [3 3 3]'
M = 2 * ones(3)
M = [M, col]
col = 3 3 3 M = 2 2 2 2 2 2 2 2 2 M = 2 2 2 3 2 2 2 3 2 2 2 3
B = 2 * eye(3)
row = [1 2 3 4 5 6 7 8 9 10]
M = [row; row; row]
M = [B, M]
B = Diagonal Matrix 2 0 0 0 2 0 0 0 2 row = 1 2 3 4 5 6 7 8 9 10 M = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 M = 2 0 0 1 2 3 4 5 6 7 8 9 10 0 2 0 1 2 3 4 5 6 7 8 9 10 0 0 2 1 2 3 4 5 6 7 8 9 10
a = [2 2 2 2 2]
b = [-1 -1 -1 -1]
C = diag(a)
D = diag(b, -1)
E = diag(b, 1)
F = C + D + E
a = 2 2 2 2 2 b = -1 -1 -1 -1 C = Diagonal Matrix 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 D = 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 0 0 0 -1 0 E = 0 -1 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 0 0 0 F = 2 -1 0 0 0 -1 2 -1 0 0 0 -1 2 -1 0 0 0 -1 2 -1 0 0 0 -1 2
row = [2 2 3 3]
base = zeros(2)
block = 5 * eye(2)
D = [row; row; base base; base block]
row = 2 2 3 3 base = 0 0 0 0 block = Diagonal Matrix 5 0 0 5 D = 2 2 3 3 2 2 3 3 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 5
n = 0 : 101;
sum(n)
a = 0;
for(i = 0:101)
a += i;
endfor
a
ans = 5151 a = 5151
function retval = transform(n)
if (n > 1)
if (mod(n, 2) == 0)
retval = n./2;
else
retval = 3 .* n + 1;
endif
else
error ("expecting value > 1");
endif
endfunction
transform(4)
ans = 2
In mathematics, the Fibonacci numbers, commonly denoted $Fn$, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from $0$ and $1$. That is,
$$ F_{0}=0, F_{1}=1 $$and
$$ F_{n} = F_{n-1} + F_{n-2} $$for $n > 1$.
function retval = fibonacci_rec(n)
if(n == 0)
retval = 0;
elseif(n == 1)
retval = 1;
else
retval = fibonacci_rec(n - 1) + fibonacci_rec(n - 2);
end
end
function f_0 = fibonacci(n)
f_1 = 1;
f_2 = 0;
i = 1;
while i < n
f_0 = (f_1) + (f_2);
f_2 = f_1;
f_1 = f_0;
i = i + 1;
end
end
tic()
fibonacci_rec(24)
elapsed_time = toc()
tic()
fibonacci(24)
elapsed_time = toc()
ans = 46368 elapsed_time = 1.0456 ans = 46368 elapsed_time = 0.0019200
An algorithm for computing the midpoint
beta = 10;
t = 2;
a = 0.96e-01;
b = 0.99e-01;
The first algorithm uses: $$a + \frac{b-a}{2} $$
c = a + b
# a = vpa(a, 2)
c = 0.19500
This means that the result obtained with this equation does not belong to the range $[0.096-0.099]$ and thus is incorrect. Another way of computing the midpoint is this: $$a + \frac{b- a}{2} $$
d = a + (b - a) / 2
d = 0.097500
This time a correct result is obtained, considering the approximation of $fl_a$.
The Algorithm of Archimedes for computing $\pi$. This algorithm provides approximations to $\pi$ through the use of the succession $\{A_i\}_{i = 1,...,n}$. This succession computes the
L'algoritmo di Archimede per il calcolo di pigreco implementato dalla funzione archimede(n)
soffre di instabilità numerica poiché il valore di pigreco diverge in modo progressivamente maggiore dal valore reale di pigreco a partire dal passo n = 15
.
In particolare si nota che il valore dell'errore relativo subisce un brusco aumento a partire dal passo n = 16
.
function A=archimede(n)
b(1)=2;
s(1)=1;
for i=1:n
A(i)=b(i)*s(i);
s(i+1)=sqrt( (1-sqrt(1-s(i)^2) )/2 );
b(i+1)=2*b(i);
end
end
La funzione myarchimede(n)
rappresenta una variante dell'algoritmo di Archimede che riesce a porre rimedio all'instabilità di cui soffriva la precedente versione. Ciò è dovuto alla diversa strategia adottata per il calcolo del valore della cella i-esima del vettore s
.
L'espressione:
$$\sqrt{\frac{1-\sqrt{1-s^2_{t-1}}}{2}}$$si riformula in:
$$\frac{s_{i-1}}{\sqrt{2 \left( 1+\sqrt{1-s^2_{i-1}} \right)}}$$function A=myarchimede(n)
b(1)=2;
s(1)=1;
for i=1:n
A(i)=b(i)*s(i);
s(i+1)=s(i)/sqrt( 2*(1+sqrt(1-s(i)^2)) );
b(i+1)=2*b(i);
end
end
n = 24;
A=archimede(n);
err=abs(A-pi)/pi; % formula per il calcolo dell'errore relativo di A
Anew=myarchimede(n);
errnew=abs(Anew-pi)/pi; % formula per il calcolo dell'errore relativo di A*
figure(1)
semilogy(1:n,err,'ks-',1:n,errnew,'ro-')
legend('A','A*')
box off
[A.' Anew.']
ans = 2.0000 2.0000 2.8284 2.8284 3.0615 3.0615 3.1214 3.1214 3.1365 3.1365 3.1403 3.1403 3.1413 3.1413 3.1415 3.1415 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1417 3.1416 3.1418 3.1416 3.1425 3.1416
format long
pi
ans = 3.141592653589793
k = 1/3; # 1/2, 1, 2
x = -pi : pi;
y = sin(k*x);
figure(1)
plot( x,sin(1/2*x),'ks-',
x,sin(1/3*x),'ro-',
x,sin(x), 'bs-',
x,sin(2*x),'ks-')
legend('1/2','1/3', '1', '2')
box off
disp('Lo script grafica una funzione scelta');
disp('dall"utente tra le seguenti:');disp(' ');
disp(' 1) y = x^3-3x per -3 <= x <=3');
disp(' 2) y = 3x cos(2x) per 0 <= x <= 2pi');
disp(' 3) y = sin(x)/x per -8pi <= x <= 8pi');disp(' ');
scelta = input('Scegliere una tra le funzioni indicate, digitandone il numero: ');
switch scelta
case 1
f=@(x) x.^3-3*x;
x = [-3:0.05:3];
y = f(x);
plot(x,y,'r')
box off
xlabel('x'); ylabel('y');
title('Grafico della funzione 1');
legend('y=x^3-3*x');
case 2
f=@(x) 3*x.*cos(2*x);
x = [0:pi/50:2*pi];
y = f(x);
plot(x,y,'b')
box off
xlabel('x'); ylabel('y');
title('Grafico della funzione 2');
legend('y=3*x*cos(2x)');
case 3
f=@(x) sin(x)./x;
x = [-8*pi:pi/50:8*pi];
y = f(x);
plot(x,y,'m')
box off
xlabel('x'); ylabel('y');
title('Grafico della funzione 3');
legend('y=sin(x)/x');
end
Lo script grafica una funzione scelta dall"utente tra le seguenti: 1) y = x^3-3x per -3 <= x <=3 2) y = 3x cos(2x) per 0 <= x <= 2pi 3) y = sin(x)/x per -8pi <= x <= 8pi
Pa = [-6, -6, -7, 0, 7, 6, 6, -3, -3, 0, 0, -6;
-7, 2, 1, 8, 1, 2, -7, -7, -2, -2, -7, -7];
row1 = Pa(1,:);
row2 = Pa(2,:);
subplot(2,2,1)
plot(row1, row2)
box off
alpha_vett=[pi/2, pi/3, pi];
for k=1:length(alpha_vett)
alpha = alpha_vett(k);
% rotazione in senso antiorario di angolo alpha
% A=[cos(alpha) -sin(alpha); sin(alpha) cos(alpha)];
% rotazione in senso orario di angolo alpha
A=[cos(-alpha) -sin(-alpha); sin(-alpha) cos(-alpha)];
subplot(2,2,k+1);
Prot = A * Pa;
plot(Prot(1,:),Prot(2,:))
box off
end