Matematičko modeliranje u biologiji

Uvod u Mathematicu

Pokretanje Mathematice

Jezgra, grafičko sučelje i bilježnice

Osnovne računske operacije

2 3-2*3

2^3

4/6

(* 4/6 *)

4./6

IntegerQ[2]

IntegerQ[2.]

Sin[2]

Sin[2.]

InputForm[%]

N[Sin[2],20]

N[Sin[2.],20]

Sin[#]&[2.]

Sin[#1-#2]&[3.,1.]

?*Sin*

Sqrt[2.]

(Sqrt[2.])

E^(-5)

E^(-5.)

Exp[-5.]

(3+I)/(4-I)

(-8)^1./3

(-8.)^1/3

(-8)^(1./3)

((-8.)^(1/3))

((-8.)^(1/3))

Simbolički račun

izraz=Expand[(a+b)^2]

Factor[izraz]

PowerExpand[Sqrt[x^2]]

Apart[x^3/(x^2-1)]

Together[%]

TrigFactor[Sin[2x] Cos[3x]]

TrigReduce[Sin[2x] Cos[3x]]

D[f[x] g[x],x]

D[Sin[x],x]

Sin'[x]

Integrate[Cos[x],x]

D[x^4,x] /.x->2

Crtanje, animacija i zvuk

Crtanje.

Plot[Sin[x],{x,0,2Pi}]

Options[Plot]

Plot[{Sin[x],Cos[x]},{x,0,2Pi},
	          PlotStyle->{RGBColor[0,1,0],
		     	{RGBColor[1,0,0],Thickness[0.03]}}]

Plot[Abs[x],{x,-2,2},   
	 Epilog->{Circle[{0,1},1]},AspectRatio->Automatic,
   Ticks->{Range[0,2,0.5],{0.2,0.6,1.8}}]

slika1=Plot[Sin[x],{x,0,2Pi},DisplayFunction->Identity];
slika2=Plot[x^2,{x,0,2},DisplayFunction->Identity];
Show[GraphicsRow[{slika1,slika2}]]

Plot[Evaluate[D[Sin[x],x]],{x,0,2Pi}]

Plot[x+Sin[2 π x],{x,0,25}]

Plot[x+Sin[2 π x],{x,0,25},PlotPoints->50]

PolarPlot[Exp[Cos[t]]-2  Cos[4t],{t,0,2Pi}]

Animacija.

Table[Plot[Sin[k x],{x,0,2Pi},PlotLabel->"sin",
		AxesLabel->{"x"," "}],{k,1,2,0.2}];

Zvuk.

l:=First[RealDigits[N[Pi,5000]]]

ListPlay[l]

Play[Sin[1000/t],{t,0,2}];

Definiranje funkcija

f[x]=x

f[3]

Clear[f]

f[x_]=x

f[3]

Clear[f];
f[x_]=Expand[(1+x)^2]

g[x_]:=Expand[(1+x)^2]

f[3]

g[3]

?f

?g

h[x_,y_]:=x+y

h[2,3]

t[s_,v_]:={s*v,s+v}

t[2,3]

Clear[g];
g[x_]:=x -2/;x>=1
g[x_]:=x^2/; x<1

g[3]

g[-3]

Clear[f,g]

f[0]=1;
f[x_]:=x f[x-1];

g[0]=1;
g[x_]:=g[x]=x g[x-1]

f[3]

g[3]

?f

?g

$RecursionLimit=3000

f[2000];

g[2000];

Timing[f[2001]][[1]]

Timing[g[2001]][[1]]

Clear[f];
f[n_]:=Plot[Sin[x],{x,0,n Pi}]

f[4];

Liste

lista={2,5,-2,1}

lista[[2]]

First[lista]

Sort[lista]

MemberQ[lista,4]

ReplacePart[lista,a,1]

l=Table[i^2,{i,6}]

Partition[l,2]

Join[lista,l]

Table[2,{5}]

tocke=Table[{x,Sin[x]},{x,0,2Pi,0.5}]

Dimensions[tocke]

tocke[[2]]

tocke[[2]][[1]]

ListPlot[tocke,PlotStyle->PointSize[0.03]]

mat1={{1,2},{3,1}};
mat2={{0,1},{2,-2}};

mat1+mat2

mat1*mat2

produkt1=mat1.mat2 //MatrixForm

produkt1+mat1

MatrixForm[produkt2=mat1.mat2]

produkt2+mat1

MatrixPower[mat1,10]

Jednadžbe

Solve[2x-3.==4,x]

s=Solve[{y==2 x-3,y+x==4},{x,y}]

y=y/.s[[1]][[2]]

rjesenje=Solve[x^2-x-1==0,x]

N[x/.rjesenje[[2]]]

Solve[x^3==-8.,x]

Solve[x^2-Cos[x]==0,x]

FindRoot[x^2-Cos[x]==0,{x,0.1}]

FindRoot[x^2-Cos[x]==0,{x,-0.1}]

FindRoot[x+Cos[x]==0,{x,Pi/2}]

D[x+Cos[x],x]/.x->Pi/2

Paketi

Quit

PolarPlot[ Cos[4t],{t,0,2Pi}]

?PolarPlot

Remove[PolarPlot]

?PolarPlot

PolarPlot[ Cos[4t],{t,0,2Pi}]

(* sadržaj paketa tečaj.m *)
      BeginPackage["tecaj`"]
      nar::usage = " "
      Begin["`Private`"]
      nar[x_]:=10 x
      End[]
      EndPackage[]

<<g:\math\nastava\tecaj.m

nar[5]

Context[nar]

Remove[nar]

Context[nar]

Vizualizacija podataka

{12,12,11,12,10,11,10,4}>>dat1;
{12,14,15,12,13,12,14}>>dat2;

<<dat1

ListPlot[{<<"dat1",<<"dat2"},Joined->{True,True},PlotStyle->{RGBColor[0,1,0],RGBColor[1,0,0]}]

podaci=Table[RandomInteger[{1,5}],{i,1,15}]

BarChart[Reverse[Sort[Tally[podaci]],2]]

Definiranje funkcija - napredniji primjeri

f[x_Integer]:=x+5;
f[x_Real]:=x^2;
f[x_Symbol]:=x-1;
f[x_List]:=x[[3]];

f[3]

f[3.]

f[a]

f[{3,1,6,8}]

lista={1,3,-5,7,-22,47,8};

Select[lista,#≤2&]

Select[lista,PrimeQ[#]==True &]

Map[#/5. &, {12,28,347,3}]

?FoldList

FoldList[10 #1 + #2&, 0, {1, 9, 2, 8,1}]

For[i=1,i<4,i++;Print[i,"  ",RandomReal[]]]

a=3;
dat=OpenWrite["dat"];
Write[dat,a];
Write[dat,6.];
For[i=1,i<=3,i++,
	     Write[dat,i^2]
];
Close[dat];

OpenRead["dat"];

Read["dat"]

t=x;Do[t=1/(1+t),{3}];t

x=1

Module[{x=4,y=6},x=x+y ;Print["x = ",x]]

?x

Clear[f];
f[lista_]:=Module[{x,n=Length[lista]},
                                         x=Apply[Plus,lista]/n];

f[{5,3,2}]

Timing[Plot[Sin[(1/x)] x,{x,-0.2`,0.2`},PlotPoints->1000,Compiled->False]]

Timing[Plot[Sin[1/x] x,{x,-0.2,0.2},PlotPoints->1000,
DisplayFunction->Identity];]

newt[x_,n_]:=Module[{t},t=x;Do[t=(t+x/t)/2.,{n}];t]

Timing[newt[2,2000]]

newtc=Compile[{x,{n,_Integer}},
Module[{t},t=x;Do[t=(t+x/t)/2.,{n}];t]]

Timing[newtc[2,2000]]

Liste - napredniji primjeri

lista={0.7I,2,1-0.2I,1.5+2I}

Transpose[{Re[lista],Im[lista]}]

clp[l_,o_]:=ListPlot[Transpose[{Re[l],Im[l]}],
		o,
		AxesLabel->{"Re","Im"},AspectRatio->Automatic,PlotStyle->PointSize[0.025]]

clp[lista]

clp[lista,PlotJoined->True];

Clear[lista];
lista=Table[{i,j},{i,1,3},{j,5,7}]

Flatten[lista]

Union[%]

lista={2,-3,{4}};

Insert[lista,novi,2]

Prepend[lista,novi]

Append[lista,novi]

x={{1,0},{2.5, .5},{3,0},{0,2},{-1,0.}};
DeleteCases[x, {_, 0 | 0.0}]

Sustavi linearnih i nelinearnih jednadžbi

Sustavi lineranih jednadžbi.

a={{1,-1},{2,-1}};
b={-1,0};

Det[a]!=0

LinearSolve[a,b]

Sustavi nelinearnih jednadžbi.

Clear[x]; sustav={x^2+y^2==1,y-Exp[x]==0};

ImplicitPlot[sustav,{x,-1.2,0.4}];

NSolve[sustav,{x,y}]

FindRoot[sustav,{x,0.5},{y,0.5}]

FindRoot[sustav,{x,-0.5},{y,0.5}]

FindRoot[sustav,{x,0.0},{y,0.0}];

Det[{{D[x^2+y^2,x],D[x^2+y^2,y]},{D[y-Exp[x],x],
D[y-Exp[x],y]}}]/. x->0.0/.y->0.0

FindRoot[sustav,{x,0.5},{y,0.5},WorkingPrecision->20]

Diferencijalne jednadžbe

Clear[y,t]

DSolve[y''[t]-5y'[t]+6y[t]==Exp[t],y[t],t]

Clear[y];
rjesenje=DSolve[{y''[t]-5y'[t]+6y[t]==Exp[t],y[0]==1,
y'[0]==2},y[t],t]

h[t_]=N[y[t]/.rjesenje[[1]]]

Plot[h[t],{t,-1,2}]

DSolve[{x'[t]+y'[t]==t,x'[t]-3y'[t]==t t},
            {x[t],y[t]},t]

DSolve[D[y[x, t], t, t] == D[y[x, t],{x,2}],
 y, {x,t}]

D[x^4 y^2,{x,2},{y,1}]

D[x^4 y^2,{x,2},{y,1}][1,2]

f[x_,y_]:=x^4 y^2

Derivative[2,1][f][1, 2]

Clear[y]

r=NDSolve[{y''[t]-5y'[t]+6y[t]==Exp[t],y[0]==1,y'[0]==2},
y,{t,0,2}]

Plot[y[t]/.r,{t,0,2}]

s=NDSolve[{D[y[x, t], t, t] == D[y[x, t],{x,2}],
    y[x, 0] == Exp[-x^2], Derivative[0,1][y][x, 0] == 0,
       y[-5, t] == y[5, t]}, y, {x, -5, 5}, {t, 0, 5}, WorkingPrecision->20]

Plot3D[Evaluate[y[x,t] /. First[s]],{x,-5,5},{t,0,5},
PlotPoints->30]

Aproksimacije funkcija

tocke=Table[{x,Sin[x]},{x,0,Pi/2,0.1}];

pravac=Fit[tocke,{1,x},x]

Plot[pravac,{x,0,Pi/2},
Epilog->{PointSize[0.03],Point/@ tocke}]

parabola=Fit[tocke,{1,x,x^2},x]

Plot[parabola,{x,0,Pi/2},
Epilog->{PointSize[0.03],Point/@ tocke}]

eksp=NonlinearFit[tocke,a*Exp[b*x],{x},{a,b}]

Plot[eksp,{x,0,Pi/2},Epilog->{PointSize[0.03],Point/@ tocke}]

<<"Splines`"

spline=SplineFit[tocke,Cubic]

spline[10]

ParametricPlot[spline[x],{x,0,15},Compiled->False,Epilog->{PointSize[0.03],Point/@ tocke}]

?*Interpolati*