Obične diferencijalne jednadzbe

MODULI

\(\(\(\ \)\(Euler[a_, b_, h_, f_, y0_] := \ 
      Module[{i, x, y}, 	n = Floor[\((b - a)\)/h]; 
	
        If[IntegerQ[n], n = n, n = n + 1]; \ 
        tab = Table[{a + \((i - 1)\)\ h, 0}, {i, n + 1}]; 
x = a; y = y0; 
        tab[\([1, 2]\)] = y0; 
			Do[
				y = y + f[x, y]\ h; \ 
          x = x + h; 
          tab[\([i, 2]\)] = y
			, {i, 2, 
            n + 1}]
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]


    RK[a_, b_, h_, f_, y0_] := 
      Module[{i, x, y, k1, k2, k3, k4}, 	
		n = 
          Floor[\((b - a)\)/h]; \ \ If[IntegerQ[n], n = n, n = n + 1]; \ 
        tab = Table[{a + \((i - 1)\)\ h, 0}, {i, n + 1}]; 
\ \ \ \ x = a; 
        y = y0; tab[\([1, 2]\)] = 
          y0; 
			Do[
			\ \ \ \ \ \ \ k1 = h\ f[x, y]; \ 			k2 = 
            h\ f[x + h/2, y + k1/2]; 
\ \ \ \ \ \ \ 			k3 = 
            h\ f[x + h/2, y + k2/2]; \ 	k4 = 
            h\ f[x + h, y + k3]; 
			y = 
            y + \((1/6)\) \((k1 + 2\ k2 + 2\ k3 + k4)\); \ 		\ tab[\([i, 
                2]\)] = y; 
			x = x + h, {i, 2, 
            n + 1}]
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)\)\)

Koristenje gotovog Mathematica-programa

Clear[x, y];
f[x_, y_] := x + y;         yy[x_] := 
      2  Exp[x] - x - 1;
NDSolve[ {y'[x]  ==  f[x, y[x]], y[0] == 1},  
      y[x], {x, 0, 1} ]
tab = 
        Table[{x, Evaluate[ y[x]  /.  % ], yy[x]},  {x,  0,  
            1,  .1}];
Print[tab // TableForm]

Primjer 1. Eulerove metode

a = 0; b = 1; h =  .1; y0 = 1;
f[x_, y_] := x + y;              sol[x_] := 
      2  Exp[x] - x - 1;
Euler[a, b, h, f, y0]; 
Print[
      TableForm[
        Table[{k, tab[[k, 1]], tab[[k, 2]],  sol[a + (k - 1) h], 
            tab[[k, 2]] -  sol[a + (k - 1) h]}, {k, 
            n + 1} ]]]

\(\(\( (*\ Primjer\ 1. \ RK\ metode\ *) \)\(
\)\(a = 0; b = 1; h =  .1; 
    y0 = 1;

    f[x_, y_] := x + y; \ \ \ \ \ \ \ \ \ sol[x_] := 2  Exp[x] - x - 1;

    \(RK[a, b, h, f, y0];\)\ 

    Print[
      TableForm[
        Table[{k, tab[\([k, 1]\)], tab[\([k, 2]\)], \ sol[a + \((k - 1)\) h], 
            tab[\([k, 2]\)] - \ sol[a + \((k - 1)\) h]}, {k, 
            n + 1}\ ]]]\)\)\)

\(\(\( (*\ Primjer\ 2. \ Eulerove\ metode\ *) \)\(
\)\(a = 0; b = 1; 
    h =  .1; y0 = 0;

    \(sol[x_] := 30 \((30\ Sin[x] - \ Cos[x] + \ Exp[\(-30\) x])\)/901;\)

    \(f[x_, y_] := 30 \((Sin[x] - y)\);\)\ 

    \(Euler[a, b, h, f, y0];\)\ 

    Print[
      TableForm[
        Table[{k, tab[\([k, 1]\)], tab[\([k, 2]\)], \ sol[a + \((k - 1)\) h], 
            tab[\([k, 2]\)] - \ sol[a + \((k - 1)\) h]}, {k, 
            n + 1}\ ]]]\)\)\)

\(\( (*\ Primjer\ 2.\ RK\ metode\ *) 
a = 0; b = 1; h = .1; y0 = 0; 

    sol[x_] := 30 \((30\ Sin[x] - \ Cos[x] + \ Exp[\(-30\) x])\)/901; 

    f[x_, y_] := 30 \((Sin[x] - y)\); \ 
RK[a, b, h, f, y0]; \ 

    Print[TableForm[
        Table[{k, tab[\([k, 1]\)], tab[\([k, 2]\)], \ sol[a + \((k - 1)\) h], 
            tab[\([k, 2]\)] - \ sol[a + \((k - 1)\) h]}, {k, n + 1}\ ]]]\)\)

\(\( (*\ Primjer\ 3.\ RK\ metode\ *) 
a = 0; b = 1; h = .1; y0 = 0; 

    sol[x_] := nema; 
f[x_, y_] := x\ y^2 + 1; \ 
RK[a, b, h, f, y0]; \ 

    Print[TableForm[
        Table[{k, tab[\([k, 1]\)], tab[\([k, 2]\)], \ sol[a + \((k - 1)\) h], 
            tab[\([k, 2]\)] - \ sol[a + \((k - 1)\) h]}, {k, n + 1}\ ]]]\)\)

<< Graphics`ImplicitPlot`

c = 1;
ImplicitPlot[(x - c/2)^2 +  y^2 == (c/2)^2, {x, 0, 
          c}];

sl[c_] := 
        ImplicitPlot[(x - c/2)^2 +  y^2 == (c/2)^2, {x, 0, c}, 
          DisplayFunction -> Identity];

alpha = -20; beta = 20; korak = 1;
Show[Table[sl[c], {c, alpha, beta, korak}], 
        DisplayFunction -> $DisplayFunction];