Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tölvunarfræði Kraftbendilsglærur Vikublað 12. Dæmi 1a.

Similar presentations


Presentation on theme: "Tölvunarfræði Kraftbendilsglærur Vikublað 12. Dæmi 1a."— Presentation transcript:

1 Tölvunarfræði Kraftbendilsglærur Vikublað 12

2 Dæmi 1a

3 % I = simpson(@f, a, b, n) finnur heildið af f frá a til b með Simpsonsreglu og % n hlutbilum. function I = simpson(f, a, b, n) h = (b-a)/n; S1 = 0; S2 = 0; for i = 1:n yi = a + (i-0.5)*h; S1 = S1 + f(yi); end for i = 1:n-1 xi = a + i*h; S2 = S2 + f(xi); end I = (h/6)*(f(a) + 4*S1 + 2*S2 + f(b)); end

4 Dæmi 1b Athugum að markgildið af sin(x)/x þegar x stefnir á 0 er 1 svo það er vandalaust að nálga svarið með því að heilda frá ε til pi/2. Með num2str(simpson(f,eps,pi/2,500),'%.5f') fæst að með 5 aukastöfum er heildið 1.37076. Með því að prófa sig áfram með mismunandi n komumst við að því að 4 hlutbil duga til að fá þessa 5 aukastafi rétta, en n = 3 gefur 1.37077. Til að fá þetta rétt með heildinu úr sýnidæmi 23 reynist þurfa n = 108 hlutbil.

5 Dæmi 7.1 Búa til töflu yfir horn Tafla yfir sin(x) á 10 mínútna bili x ° 0' 10' 20' 30' … 0 0.0000 0.0029 0.0058 0.0087 1 0.0175 0.0204 0.0233 0.0262 2 0.0349 0.0378 0.0407 0.0436 3 0.0523 0.0552 0.0581 0.0610 …

6 Dæmi 7.1 for i=1:90 for j=1:6 G(i,j) = (i-1) + (j-1)/6; end sinG = sin(G); fprintf('Tafla yfir sin(x) á 10 mínútna bili\n'); fprintf('xº 0'' 10'' 20'' 30'' 40'' 50''\n'); for i=1:90 gradur = i-1; fprintf('%-3d', gradur) fprintf('%8.4f', sinG(i,:)) fprintf('\n') end

7 Næst

8 9.1 Write a command script that asks the user for a number n and subsequently displays a Pascal triangle with n lines. This is a Pascal triangle with 4 lines: 1 1 1 1 2 1 1 3 3 1

9 9.3 9.3Given a function f and and interval [a, b] such that the sign of f at a is different form the sign at b then the bisection method for solving f (x) = 0 with accuracy e is described by the following algorithm: loop while b – a < 2e m  if sign of f at a is different from sign of f at m then b  m else a  m end-if end-loop x  Write a matlab function that solves f (x) = 0 with this method. The function shall have the function f, the interval [a, b], and the accuracy required eps as parameters. It should return the solution, the needed number of iterations (n). The function head could for example be [x,n] = bisection(f,a,b,eps). Try the function by determining the three smallest solutions to the equation x + 1 = tan x.


Download ppt "Tölvunarfræði Kraftbendilsglærur Vikublað 12. Dæmi 1a."

Similar presentations


Ads by Google