National and Kapodistrian University of Athens

Department of Economics
Division of Mathematics and Informatics
MTH302 Dynamical Mathematics with Mathematica
T.A. PhD candidate Nick Poulios
Email: npoulios@econ.uoa.gr
Solutions to Assignment 5

Exercise 1

pinakas=Table[RandomInteger[{1,100}],50]

maximum=pinakas[[1]]

Do[If[pinakas[[i]]>maximum,maximum=pinakas[[i]]] ,{i,1,50}]

maximum

Print["The maximum number is: ",maximum]

Exercise 2

max=pinakas[[1]]

index=1

For[i=1,i≤50,i++,If[pinakas[[i]]>max,max=pinakas[[i]];index=i]]

Print["The maximum number of random list is ",max," at position ",index ]

Exercise 3

a)

fnca[x_]:=x^5-Cos[x]-x^3+2 x-4

fncb[x_]=D[fnca[x],x]

xroot[n_Integer]:=xroot[n-1]-fnca[xroot[n-1]]/fncb[xroot[n-1]]

xroot[1]=1

mat1=Table[xroot[n],{n,1,8}]//N

FindRoot[fnca[x],{x,1}] (*compare with FindRoot*)

Exercise 4

a)

a[n_Integer]:=2*a[n-1]/n

a[1]=4;

mat=Table[a[n],{n,1,10}]

ListPlot[mat,Filling->Axis,ImageSize->500]

Αντίστοιχα για τα υπόλοιπα.

Exercise 5

20! (*Για επιβεβαίωση*)

(*Case: Do*)

fact[0]=1;

Do[fact[n_]=n fact[n-1],{n,1,20}];Print[fact[n]]

(*Case: While*)

fact1[0]=1;n=0;While[n≤20,fact1[n_]=n fact1[n-1];n+=1];Print[fact1[n]]

(*Case: For*)

fact2[0]=1;

For[n=1,n≤20,n=n+1,fact2[n_]=n fact2[n-1]];Print[fact2[n]]

Exercise 6

a)

mata={{1,0,1},{0,1,-1},{1,-2,0}};
matb={{1,0,1},{0,1,-1},{1,-2,0}};
matc={{1,1,1},{0,0,-1},{1,1,0}};
matd={{1,0,1},{0,1,0},{1,-2,1}};

detSystem=Det[mata]

x1=Det[matb]/detSystem

x2=Det[matc]/detSystem

x3=Det[matd]/detSystem

Exercise 7

ContourPlot[{Exp[x]+Log[y]==2,Sin[x]+Cos[y]==1},{x,0,3},{y,0,3}]

FindRoot[{Exp[x]+Log[y]==2,Sin[x]+Cos[y]==1},{x,0.5},{y,0.5}]

Exercise 8

sol=DSolve[{y'[x]==x*y[x],y[1]==2},y[x],x]

Plot[y[x]/.sol,{x,-2,2}]

Exercise 9

vf=VectorPlot[{1,x^2+y},{x,0,1},{y,0,12},Axes->Automatic,AspectRatio->1/GoldenRatio,DisplayFunction->Identity]

sol1=Table[DSolve[{y'[x]==x^2+y[x],y[0]==k},y[x],x],{k,0,4}];

Do[h[k]=Plot[sol1[[k,1,1,2]],{x,0,1},PlotRange->All,DisplayFunction->Identity],{k,1,5}]

Show[h[1],h[2],h[3],h[4],h[5],vf,DisplayFunction->$DisplayFunction,ImageSize->500]

Exercise 10

DSolve[{y''[x]+5 y'[x]+6 y[x]==0,y[0]==2,y'[0]==3},y[x],x]

Exercise 11

a[n_Integer]:=If[n==1,1,a1=1.0;Do[a1=2/3(a1+3/a1^2),{n-1}];a1]

Do[Print[a[i]],{i,1,10}]

Exercise 12

sum=0;
For[i=1,i≤10,++i,sum=sum+i];
sum