Notebook zu: Mathematik für Informatiker
Band 2

©2001-2007 Susanne Teschl  <Susanne.Teschl@technikum-wien.at> und Gerald Teschl  <Gerald.Teschl@univie.ac.at>

Dieses Notebook ist als Ergänzung zu unserem Buch Mathematik für Informatiker, Band 2, Springer 2007, gedacht. Dort finden Sie auch weitere Erklärungen.

Elementare Funktionen

Nullstellen

Solve[(x^3)-7(x^2)+7x-1==0,x]

Faktorisierung

Factor[(x^3)-1]

Expand[%]

Polynomdivison

PolynomialQuotient[3(x^4)+(x^3)-2x,(x^2)+1,x]

PolynomialRemainder[3(x^4)+(x^3)-2x,(x^2)+1,x]

Interpolationspolynome

f[x_]=(1/(1+25 (x^2)));

Stuetzpunkte[n_]:=Table[{(j/n),f[((j )/n)]},{j,-n,n}]

P[x_]=InterpolatingPolynomial[Stuetzpunkte[6],x]//Expand

Plot[{f[x],P[x]},{x,-1,1},PlotRange->All,PlotStyle->{Black,GrayLevel[0.5`]}]

Graphische Darstellung von Funktionen

Plot[Sin[x],{x,0,2 π},Ticks->{{0,(π/2),π,((3 π)/2),2 π},Automatic}]

Plot[{Sin[x],Sin[2 x]},{x,0,2 π},Ticks->{{0,π,2 π},Automatic},PlotStyle->{{},Dashing[{0.05`,0.02`}]}]

Plot[{Sin[x],Sin[2 x]},{x,0,2 π},Ticks->{{0,π,2 π},Automatic},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,1]}}]

Bogenmaß und Gradmaß

Sin[(π/2)]

Sin[90 Degree]

Sin[90.]

Funktionen mit komplexen Argumenten, Polardarstellung

Sin[I]

Exp[2 + I (π/3)]//ComplexExpand

{Abs[1+I (Sqrt[3])],Arg[1+I (Sqrt[3])]}

Sqrt[1 + I(Sqrt[3])]//ComplexExpand

((8Exp[I π/2])^(1/3))//ComplexExpand

Differentialrechnung  I

Grenzwert

Limit[(((x^2)-1)/(x-1)),x->1]

Ableitungen

D[2 (x^3)-4x+1,x]

D[2 (x^3)-4x+1,{x,2}]

f[x_]:=2 (x^3)-4x+1; f'[x]

Clear[f]

Splines

Ein kleines Programm zur Berechnung von natürlichen Splines:

NaturalSpline[xy_,t_]:=Module[{x,y,n,u,v,d,k,Fh,F,spl},
{x,y}=Transpose[xy];
n=Length[x];u=Table[0,{j,n}];v=Table[0,{j,n}];
d=Table[x[[j+1]]-x[[j]],{j,n-1}];
k=Table[((y[[j+1]]-y[[j]])/(d[[j]])),{j,n-1}];
u[[1]]=0;u[[2]]=3 k[[1]];v[[1]]=1;v[[2]]=-2;
Fh[j_,z_]:=((-1)/(d[[j-2]]))(2(d[[j-1]]+d[[j-2]])z[[j-1]]+ d[[j-1]] z[[j-2]]);
F[j_,z_]:=Fh[j,z]+(3/(d[[j-2]]))(k[[j-2]]d[[j-1]]+k[[j-1]]d[[j-2]]);
Do[u[[j]]=F[j,u];v[[j]]=Fh[j,v];,{j,3,n}];
z=u+ ((3k[[n-1]]-u[[n-1]]-2u[[n]])/(2v[[n]]+v[[n-1]])) v;
spl=Flatten[Table[{t<x[[j+1]],Expand[y[[j]]+k[[j]](t-x[[j]])+((z[[j]]-k[[j]])/((d[[j]])^2))(t-x[[j]])(((t-x[[j+1]]))^2)+((z[[j+1]]-k[[j]])/((d[[j]])^2))(((t-x[[j]]))^2)(t-x[[j+1]])]},{j,n-1}]] ;
spl[[-2]]=True;spl [[0]]=Which;
spl
];

f[x_]=(1/(1+25 (x^2)));

Stuetzpunkte[n_]:=Table[{(j/n),f[(j/n)]},{j,-n,n}];
XY=Stuetzpunkte[6];
S[x_]=NaturalSpline[XY,x];
P[x_]=InterpolatingPolynomial[XY,x];
dots=ListPlot[XY,PlotStyle->{PointSize[0.02`]}];
gr1=Plot[f[x],{x,-1,1},PlotRange->{-1,1}];
gr2=Plot[S[x],{x,-1,1},PlotRange->{-1,1}];
gr3=Plot[P[x],{x,-1,1},PlotRange->{-1,1}];
Show[gr1,dots,AxesLabel->{"x","f[x]"}]
Show[gr2,dots,AxesLabel->{"x","Spline"}]
Show[gr3,dots,AxesLabel->{"x","Interpolationspolynom"}]

Clear[f,S,P]

Differentialrechnung II

Taylorpolynome

Series[Sin[x],{x,0,3}]

Normal[%]

Gauß'sche Glockenkurve

f[x_]:=(1/((Sqrt[2π])σ))(E^(-((((x-μ))^2)/(2(σ^2)))));

f'[x]

Solve[f'[x]==0,x]

f''[μ]

Solve[f''[x]==0,x]

{f'''[μ-σ],f'''[μ+σ]}

μ=0;σ=1;Plot[f[x],{x,-4,4}]

Sekantenverfahren

f[x_]:=x^3+2x^2+10x-20;

{f[1],f[2]}

x[n_]:=x[n-1]-f[x[n-1]]((x[n-1]-x[n-2])/(f[x[n-1]]-f[x[n-2]]))

x[0]=1.;x[1]=2.;{x[2],x[3],x[4],x[5]}

Newtonverfahren

f[x_]:=x^2-2;
x[n_]:=x[n-1]-((f[x[n-1]])/(f'[x[n-1]]))

x[0]=1.;{x[1],x[2],x[3],x[4],x[5]}

FindRoot[f[x],{x,1}]

Konvergenzbereich des Newtonverfahrens

WhichZero=Compile[{{z0,_Complex}},
Block[{z=z0,k},
For[k=0,k<200&&Abs[z^3-1]>0.5, k++,z=(1/(3 (z^2)))+((2 z)/3)];
(Round[Im[z]]+1)/2]
];
MyColor[x_]:=Which[x==0,RGBColor[1,0,0],x==1/2,RGBColor[0,1,0],x==1,RGBColor[0,0,1],True,RGBColor[1,1,1]];

DensityPlot[WhichZero[x+I y],{x,-2,2},{y,-2,2},PlotPoints->400,Mesh->False,ColorFunction->MyColor]

DensityPlot[WhichZero[x+I y],{x,-2,-1},{y,-0.5`,0.5`},PlotPoints->400,Mesh->False,ColorFunction->MyColor]

Dioden-Logik

c=39.6;

(Subscript[I, D])[U_]=10^-9(Exp[c  U] -1);

Plot[(Subscript[I, D])[U],{U,-0.5`,0.5`},PlotRange->All]

R0=10000; R1=4700;R=((R0 R1)/(R0+R1));

U0[E1_,E2_]:={E1-E2+U2,U2,E2-U2}/.
FindRoot[U2==E2 - R ((Subscript[I, D])[E1-E2+U2]+(Subscript[I, D])[U2]),{U2,-4}]

{U0[0,0],U0[5,0],U0[0,5],U0[5,5]}  //Chop//TableForm

Clear[T,(Subscript[I, D]),R,R0,R1,c,U0]

Physikalische Konstanten

<<"PhysicalConstants`"

BoltzmannConstant

ElectronCharge

T=293. Kelvin;
c=(ElectronCharge/(BoltzmannConstant T))

<<"Units`"

Convert[c,(1/Volt)]

Übugnen

f[x_]:=(x/(Sqrt[1+(x^2)]));
x[n_]:=x[n-1]-((f[x[n-1]])/(f'[x[n-1]]))

x[0]=0.5;{x[1],x[2],x[3],x[4]}

x[0]=1;{x[1],x[2],x[3],x[4]}

x[0]=2;{x[1],x[2],x[3],x[4]}

Integralrechnung

Unbestimmtes Integral

∫(Sin[x]+(x^2))\[DifferentialD]x

Bestimmtes Integral

(∫_0^1)(2(E^x)+x)\[DifferentialD]x

Uneigentliches Integral

(∫_1^∞)(1/(t^3))\[DifferentialD]t

Gammafunktion

Gamma[5]

Plot[Gamma[x],{x,0,6}]

Fourierreihen

Fourierpolynom

FourierTrigSeries[Abs[t],t,3,FourierParameters->{1,2π}]

Fourierreihe

f[t_]:=If[t>0,1,0];Plot[f[t],{t,-π,π},Ticks->{{-π,-(π/2),0,(π/2),π},Automatic}]

a[0]=(1/π)(∫_(-π)^π)f[t]\[DifferentialD]t

a[k_]=(1/π)(∫_(-π)^π)f[t]Cos[k t]\[DifferentialD]t

Simplify[%,Element[k,Integers]]

b[k_]=(1/π)(∫_(-π)^π)f[t]Sin[k t]\[DifferentialD]t

Simplify[%,Element[k,Integers]]

{b[1],b[2],b[3],b[4],b[5],b[6]}

F[n_,t_]:=FourierTrigSeries[f[t],t,n];
Plot[Evaluate[{f[t],F[3,t],F[12,t]}],{t,-π,π},PlotStyle->{{},{Dashing[{0.04`,0.02`,0.01`,0.02`}]},{}},Ticks->{{-π,-(π/2),0,(π/2),π},Automatic}]

Differentialrechnung in mehreren Variablen

Graphische Darstellung

Plot3D[2-(x^2)-(y^2),{x,-2,2},{y,-2,2}]

Plot3D[2-(x^2)-(y^2),{x,-2,2},{y,-2,2},ClippingStyle->None,PlotPoints->50,Mesh->False,PlotRange->{-1,2}]

Partielle Ableitung

D[(Subscript[x, 1])((Subscript[x, 2])^3),(Subscript[x, 1]),(Subscript[x, 1])]

D[(Subscript[x, 1])((Subscript[x, 2])^3),(Subscript[x, 1]),(Subscript[x, 2])]

Hesse-Matrix

Table[D[(Subscript[x, 1])( (Subscript[x, 2])-1) + ((Subscript[x, 1])^3),(Subscript[x, k]),(Subscript[x, j])],{k,2},{j,2}]//MatrixForm

Differentialgleichungen

Differentialgleichungen

DSolve[x'[t]==2(Sqrt[x[t]]),x,t]

DSolve[{x'[t]==2(Sqrt[x[t]]),x[0]==x0},x,t]

DSolve[{x''[t]==x'[t]+6x[t],x[0]==1,x'[0]==8},x,t]

Systeme von Differentialgleichungen

Volterra-Lotka-Gleichung

loesung= {x[t],y[t]}/.NDSolve[{x'[t]==(1-y[t])x[t],y'[t]==(x[t]-1)y[t],x[0]==0.7,y[0]==0.7},{x,y},{t,0,7}][[1]];

ParametricPlot[loesung,{t,0,7}]

Folgendes Programm berechnet die Lösungskurven für mehere Anfangswerte und stellt sie gemeinsam dar.

PhasePlot[f_,ic_,tmax_,opts___]:=Block[{i,n=Length[ic],ff,ivp,sol,phaseplot},
ff=f /. {x->x[t],y->y[t]};
Off[ParametricPlot::"ppcom"];
Do[
ivp={x'[t]==ff[[1]],y'[t]==ff[[2]],x[0]==ic[[i,1]],y[0]==ic[[i,2]]};
sol=NDSolve[ivp,{x[t],y[t]},{t,-tmax,tmax}];
phaseplot[i]=ParametricPlot[{x[t],y[t]} /. sol,{t,-tmax,tmax}]
,{i,1,n}];
On[ParametricPlot::"ppcom"];
Show[Table[phaseplot[i],{i,1,n}],opts]
];

PhasePlot[{(1-y)x,(x-1)y},{{0.3,0.3},{0.5,0.5},{0.7,0.7},{0.9,0.9},{1,1}},3.7,PlotRange->{{0,3.5},{0,3.5}},Ticks->False]

Lorenz-Gleichung

σ=10; ρ=28; β=8/3;loesung={x[t],y[t],z[t]} /.NDSolve[{x'[t]==-σ (x[t]-y[t]),y'[t]==-x[t] z[t]+ρ x[t]-y[t],z'[t]==x[t] y[t]-β z[t],x[0]==30,y[0]==10,z[0]==40},{x,y,z},{t,0,20}][[1]];

ParametricPlot3D[loesung,{t,0,20},PlotPoints->2000,Axes->False]

Beschreibende Statistik und Zusammenhangsanalysen

Stabdiagramm

Needs["BarCharts`"];

BarChart[{1/20,3/20,7/20,1/4,1/10,1/10},BarSpacing->-.05,BarGroupSpacing->.75,BarLabels->{"397","398","399","400","401","402","11","12"},AxesLabel->{"(Subscript[a, i])","(Subscript[f, i])"}]

Histogramm

Needs["Histograms`"];

Histogram[{3,7,12,18,19,20,25,25,27,28,29,31,32,34,37,38,40,41,45,47},HistogramCategories->{0,10,20,30,40,50},Ticks->{{5,15,25,35,45},{2,4,6} }]

Arithmetische Mittel

Mean[{1,2,2,2,3,3,3,3,3,4,4}]

Median

data={1,2,2,2,3,3,3,3,3,4,4};
Median[data]

Quantile[data,{0.25,0.5,0.75}]

Standardabweichung

data = {1,1,2,2,3,3,3,3,4,4,7};
{Variance[data], StandardDeviation[data]}

Streudiagramm

data={{163, 59},{165, 62},{166,65},{169,69},{170, 65}, {171, 69}, {171, 76}, {173, 73}, {174, 75},{175, 73} , {177, 80} , {177, 71},  {179, 82}, {180, 84}, {185, 81}};
ListPlot[data, PlotRange->{{160, 187},{55, 85}},AxesLabel->{Groesse x, Gewicht y},PlotStyle->PointSize[0.016]]

Korrelationskoeffizient

Needs["MultivariateStatistics`"]

xdata={30,45,45,60,75,80,90,100,110,120};
ydata={6,5,3,4,3,5,2,4,2,3};
Correlation[xdata,ydata]//N

xdata={163,165,166,169,170,171,171,173,174,175,177,177,179,180,185.};
ydata={59,62,65,69,65,69,76,73,75,73,80,71,82,84,81.};
Correlation[xdata,ydata]//N

xdata={163,165,166,169,170,171,171,173,174,175,177,177,179,180,185};ydata={2900,1100,3600,2300,4000,5600,2100,5100,3400,1800,2100,2600,4600,3600,2300};Correlation[xdata,ydata]//N

Regressionsgerade

data={{163, 59},{165, 62},{166,65},{169,69},{170, 65}, {171, 69}, {171, 76}, {173, 73}, {174, 75},{175, 73} , {177, 80} , {177, 71},  {179, 82}, {180, 84}, {185, 81}};
g=Fit[data, {1,x},x]

g1=ListPlot[data,PlotRange->{{160, 187},{55, 85}},PlotStyle->PointSize[0.016],DisplayFunction->Identity];
g2=Plot[g,{x,160,190}];Show[g1,g2, AxesLabel->{Gewicht x, Groesse y}]

Elementare Wahrscheinlichkeitsrechnung

Optimale Stoppstrategie

P[n_,j_]:=(j/n)(∑_(k=j)^(n-1))(1/k);

Table[N[P[12,j],2],{j,11}]

Zufallsvariablen

FilledPlot

f[x_]:=(2/(Sqrt[π]))(x^2) Exp[-(x^2)];

gr1=Plot[f[x],{x,-4,1},Filling->Axis];
gr2=Plot[f[x],{x,-4,3}];
Show[gr1,gr2,AxesLabel->{"t","f(t)"},PlotRange->All]

Zufallszahlen

RandomReal[ExponentialDistribution[1.5]]

n=4;xlist=RandomReal[ExponentialDistribution[1],n];
F[x_]:=1-(E^(-x));
Fn[x_]:=Count[xlist,y_/;(y≤ x)]/Length[xlist];
Plot[{Fn[x],F[x]},{x,0,10}]

Diskrete Wahrscheinlichkeitsverteilungen

Hypergeometrische Verteilung

hdist=HypergeometricDistribution[5,4,20];
{Mean[hdist], Variance[hdist]}

{PDF[hdist,2],CDF[hdist,2]}//N

Binomialverteilung

bdist=BinomialDistribution[50,0.03];PDF[bdist,2]

Poisson-Verteilung

pdist=PoissonDistribution[1.6];
CDF[pdist,2]

Stetige Wahrscheinlichkeitsverteilungen

Normalverteilung

ndist = NormalDistribution[4,2];
CDF[ndist,6]//N

p-Quantile

Quantile[NormalDistribution[100,5],0.05]

Normalverteilung als Näherung

n=(10^7);p=(10^(-5));μ=n p; σ=(Sqrt[n p (1-p)]);x=90;
1-{CDF[BinomialDistribution[n,p],x],CDF[NormalDistribution[μ,σ],x+0.5]}//N

λ=20;x=14;{CDF[PoissonDistribution[λ],x],CDF[NormalDistribution[λ,(Sqrt[λ])],x+0.5]}//N

Prüfverteilungen

Quantile[ChiSquareDistribution[12], 0.9]

Quantile[StudentTDistribution[12],0.9]

Quantile[FRatioDistribution[2,12], 0.95]

Schließende Statistik

Needs["HypothesisTesting`"]

data={100.,97.,101.,96.,98.,102.,96.,100.,101.,98.};
{Mean[data],Variance[data],StandardDeviation[data]}

Konfidenzintervall für den Erwartungswert

MeanCI[data,KnownVariance->4,ConfidenceLevel->.9]

MeanCI[data,KnownVariance->4]

MeanCI[data]

Konfidenzintervall für den Vergleich zweier Erwartungswerte

data1={152,148,149,146,146,152,149,150};
data2={153,150,149,149,152,151,154,152};
MeanDifferenceCI[data1,data2,EqualVariances->True,ConfidenceLevel->0.9]

Konfidenzintervall für die Varianz

VarianceCI[data]

Konfidenzintervall für den Anteilswert

ProportionCI[n_,x_,opts___]:=Block[{p,Fp},
p =1-((1-ConfidenceLevel)/2) /. {opts,ConfidenceLevel->.95};
Fp[m1_,m2_]:=Quantile[FRatioDistribution[m1,m2],p];
{(x/(x+(n-x+1)Fp[2(n-x+1),2x])),(((x+1)Fp[2(x+1),2(n-x)])/(n-x+(x+1)Fp[2(x+1),2(n-x)]))}];

ProportionCI[500,60]

Hypothesentests

Needs["HypothesisTesting`"]

MeanTest[data,100,KnownVariance->4,SignificanceLevel->0.05]

MeanTest[data,100,TwoSided->True,FullReport->True,SignificanceLevel->0.05]

(χ^2)-Anpassungstest auf Normalverteilung

xlist={102.5,115.7,93.8,102.6,110.,111.5,98.6,96.8,101.7,110.9,103.,104.4,108.4,104.2,97.7,105.5,92.1,100.3,97.9,105.5,106.1,90.3,108.9,96.4,90.4,91.5,94.9,99.9,84.9,102.,101.5,96.8,99.9,104.6,92.7,87.9,104.,108.6,94.7,107.3,98.6,96.6,105.4,101.4,104.,94.2,108.3,106.2,101.1,109.1,94.,95.6,100.1,89.5,101.2,94.1,92.,100.1,105.5,105.1,94.1,113.1,101.6,86.,92.1,91.5,98.6,90.6,101.4,93.6,88.3,88.5,88.6,95.9,108.2,101.2,101.1,96.9,100.2,104.7,96.6,109.2,108.5,108.4,111.6,99.2,90.8,111.7,99.7,100.4,96.,95.7,90.9,95.5,106.6,100.2,114.1,101.6,113.6,98.5};
n=Length[xlist]

{m,s}={Mean[xlist],((n-1)/n)(Sqrt[Variance[xlist]])}

ndist = NormalDistribution[m,s];

k=16;
{min,max}={Floor[Min[xlist]],Ceiling[Max[xlist]]};
clist=Table[If[j≤0,-∞,If[j≥16,∞,min+(max-min)(j/k)]],{j,0,k}]

tab=Table[{{clist[[i]],clist[[i+1]]},Count[xlist,y_/;(clist[[i]]<y≤ clist[[i+1]])],n(CDF[ndist,clist[[i+1]]]-CDF[ndist,clist[[i]]])},{i,1,Length[clist]-1}];
tab//MatrixForm

clist={-∞,90,92,94,96,98,100,102,104,106,108,110,∞};

tab=Table[{{clist[[i]],clist[[i+1]]},Count[xlist,y_/;(clist[[i]]<y≤ clist[[i+1]])],n(CDF[ndist,clist[[i+1]]]-CDF[ndist,clist[[i]]])},{i,1,Length[clist]-1}];
tab//MatrixForm

{Sum[(((tab[[i,2]])^2)/(tab[[i,3]])),{i,1,Length[clist]-1}]-n,Quantile[ChiSquareDistribution[Length[clist]-4],0.95]}