Sunteți pe pagina 1din 16

Studiul sirurilor si seriilor folosind grafica 2D si instructiunile de flux for si while

I)

CICLUL CU TEST INITIAL (repetarea unei operatii atata timp cat o


anumita conditie este indeplinita) while
>> while (condition)
body
endwhile
>>

1. Siruri numerice
Exercitiul #1

n=1:100; plot(n,(1+(1./n)).^n,'*')

>>n=1;x=abs((1+(1./n)).^n-exp(1));
>>while x>.001 n=n+1; x=abs((1+(1./n)).^n-exp(1));end
>>while x>.001 n=n+1; x=abs((1+(1./n)).^n- exp(1));endwhile
OCTAVE

% varianta

>>[n]
ans = 1359
Al 1359-lea termen aproximeaz cu dou zecimale numrul e :
n=1359;y=(1+(1./n)).^n
y=
2.7173
Exercitiul #2

n=1:30;plot(n,(n.^2+1)./3.^n,'*')

n=1;x=(n.^2+1)./3.^n; while x>.001 n=n+1;x=(n.^2+1)./3.^n; end


n=1;x=(n.^2+1)./3.^n; while x>.001 n=n+1;x=(n.^2+1)./3.^n; endwhile %
Varianta OCTAVE
[n]
ans =
11
n=11;y=(n.^2+1)./3.^n
x=
6.8869e-004
Menionm c notaia e este folosit pentru numere foarte mici, ca n cazul nostru, sau foarte
mari:
6.8869e-004=6.8869.10-4

Exercitiul #3

n=1;x=norm([(2.*n-1)./(n.^2+5),(n.^4-1)./(n.^4+8),(n.^3)./(5.^n)]-
[0,1,0]);while x>0.001
n=n+1; x=norm([(2.*n-1)./(n.^2+5),(n.^4-1)./(n.^4+8),(n.^3)./(5.^n)]-
[0,1,0]);end
n=1;x=norm([(2.*n-1)./(n.^2+5),(n.^4-1)./(n.^4+8),(n.^3)./(5.^n)]-
[0,1,0]);while x>0.001
n=n+1; x=norm([(2.*n-1)./(n.^2+5),(n.^4-1)./(n.^4+8),(n.^3)./(5.^n)]-
[0,1,0]);endwhile
% Varianta OCTAVE
[n]
ans =
2000
Aadar 1999 termeni ai irului nu aparin bilei.
II) CICLUL CU TEST INITIAL (repetarea unei operatii atata timp cat o
anumita conditie este indeplinita) for
>> for var = expression
body
endfor
>>
>>

Exercitiul #4 (siruri de functii)

x=0:.01:1/3; for n=1:16 subplot(4,4,n),plot(x,(3.*x).^n+1); end


x=0:.01:1/3; for n=1:16 subplot(4,4,n),plot(x,(3.*x).^n+1); endfor
Varianta OCTAVE

x=-3:.1:3; for n=1:6 subplot(2,3,n),plot(x,exp(x.*n)); end


x=-3:.1:3; for n=1:6 subplot(2,3,n),plot(x,exp(x.*n)); endfor % Varianta
OCTAVE

x=-2:.1:2;plot(x,exp(2.*x), x,exp(3.*x),'*', x,exp(4.*x), '--')

Exercitiul #5 (serii numerice)

n=2;S=1/3;while abs(S-3/4)>.01;n=n+1; S=S+1/(n.^2-1);end


n=2;S=1/3;while abs(S-3/4)>.01;n=n+1; S=S+1/(n.^2-1);endwhile % Varianta
OCTAVE
[n,S]
ans =
100.0000 0.7400

Alte exemple

MATLAB SYMBOLIC

Exercitiul #1

syms x
n=1;s=-(x^2)/(1+x^4);while n<50 n=n+1; s=s+((-1)^n)*x^2/(n^4+x^4);end
ezplot(s,-5,5)

Afiarea expresiei simbolice s49(x) se obine scriind [s].


[s]
ans =
-x^2/(1874161+x^4)-x^2/(2825761+x^4)+x^2/(2560000+x^4)+
x^2/(3111696+x^4)+x^2/(2085136+x^4)-x^2/(3418801+x^4)-x^2/(1+x^4)+x^2/(16+x^4)x^2/(81+x^4)+x^2/(256+x^4)-x^2/(4100625+x^4)-x^2/(625+x^4)+x^2/(1296+x^4)x^2/(2401+x^4)+x^2/(4096+x^4)-x^2/(6561+x^4)+x^2/(3748096+x^4)+x^2/(10000+x^4)-

x^2/(14641+x^4)+x^2/(20736+x^4)+x^2/(1679616+x^4)x^2/(28561+x^4)+x^2/(38416+x^4)-x^2/(50625+x^4)x^2/(5764801+x^4)+x^2/(65536+x^4)+x^2/(5308416+x^4)x^2/(83521+x^4)+x^2/(104976+x^4)-x^2/(4879681+x^4)x^2/(130321+x^4)+x^2/(160000+x^4)x^2/(194481+x^4)+x^2/(4477456+x^4)+x^2/(234256+x^4)x^2/(279841+x^4)+x^2/(331776+x^4)-x^2/(390625+x^4)+x^2/(456976+x^4)x^2/(531441+x^4)+x^2/(614656+x^4)x^2/(707281+x^4)+x^2/(6250000+x^4)+x^2/(810000+x^4)x^2/(923521+x^4)+x^2/(1048576+x^4)-x^2/(1185921+x^4)+x^2/(1336336+x^4)x^2/(1500625+x^4)-x^2/(2313441+x^4)
Exercitiul #2

S-ar putea să vă placă și