Solutions to the homeworks

Everybody

1. Listen to the video:

Hyperlink["Extremely short introduction","https://www.youtube.com/watch?v=pxx6VaUcuLo&feature=em-subs_digest"]

How do you change the style of more than one cells at the same time?

Bence Bogdola (Neat notebooks!)

It is possible to change the style of more than one cell at the same time by holding the Alt key while clicking on the cell bracket of a cell with a certain style selecting all cells with the same type or selecting the cells one-by-one holding down the control key and after the selection one can simply alternate the style and the change will apply to all the selected cells.

Carlos Henrique Cazagrande

Video and changing cells at the same time:I watched the movie and I could be able to follow the steps,I could learn a lot with the instructions in the video,and the way I learned to change the style of the cells at the same time is:press Alt and select one of the cells,the Mathematika will select all the others belong to the same style.

Mathematica

Dávid Horváth

Two ways are mentioned in the video: you can either hold down the control key and klick one by one on each square bracket or you can hold down the alt key and klick on only on square bracket and then all the cells ot that type will be selected.

István Mester

- hold the ctrl key, then select the cells that should be changed or
- hold the alt key, click on the cell with arbitrary style, and all of other cells with the same style will be selected

Tamás Mezei

Carsten Olm

a) Press and hold “Ctrl” and select the brackets of the cells you would like to format
b) For selecting all cells of the same style, press “Alt” and choose one of the cells which you would like to format

Formating can be done via the menu “Format” or with keyboard shortcuts, e.g. “Alt” + 1 (, 2, 3, ..., 9)

Marco Antonio Pederzini Pereira

To change the style of certain cells, you can hold the CTRL key and click one by one or you can hold the ALT key and click on the cell bracket in a certain style of cell and this selects all the cells of that type, and then when you change one cell, all the others change too.

Tamás Pölöskei

We can select the cells with identical style with holding down Alt key and selecting the cell bracket of the kind we would like to edit. After selecting the cells we can go to Format>Style where we can select the desired style.

Bence Tipary

András Vértesaljai

Xuan

To change the style of many cells at the same time, we first select all of the same type cells by holding “Alt” and concurrently clicking on one bracket  on the right.
Then make change by clicking on section “Format->Style”

2. Solve Problem 2 and 3 from the previous set if you have not done so as yet.

Bence Bogdola

Carlos Henrique Cazagrande

Dávid Horváth

István Mester

2: Best demonstration

Hyperlink["Ehrenfest's double-urn game","http://demonstrations.wolfram.com/EhrenfestsDoubleUrnGame/"]

It is a simple representation of statistical irreversibility. The simple but effective graphics with the unrs and balls and the plot are updated after every step depending on a random number. This demonstration shows clearly the advantages of the Mathematica dynamic module.

3: Short introdutcion

My name is István Mester, I have been studying mechanical engineering since 2010. My specialization during my bachelor' s degree was engineering development, then i' ve choosed MSc in mechanical engineering modelling. The first time I met Mathematica was the subject Basics of finite element method. I' ve found the program very useful in my studies.
  
I wrote my BSc thesis at the department of Applied Mechanics with the title Development of optical strain measurement algorithm using digital image processing technique. The main goal of the thesis was to develop a Mathematica code which follows markers on a video and makes several calculations from the data such as strain.

Tamás Mezei

Carsten Olm

It took me approximately a half minute to stumble upon a Demonstration Project that I have seen previously:

Hyperlink["Visualization of reaction networks with Volpert graphs","http://demonstrations.wolfram.com/VolpertGraph/"]

I consider using Mathematica in the analysis (and perhaps, optimization) of detailed reaction mechanisms, which is the topic of my PhD project, which is why the above example is interesting for me. Other than that I am a big fan of colorful and animated visualizations (regardless of their usefulness), such as the below:

Hyperlink["Fire","http://demonstrations.wolfram.com/SimulatingFlickeringFireWithNoisyCellularAutomaton/"]

My name is Carsten Olm, I am a PhD student of Physical Chemistry at ELTE-TTK since 2013. I moved to Hungary in 2012 and studied Chemical Engineering before starting my PhD. My experience with the above mentioned codes is limited to Matlab (somewhat, mainly from a user' s perspective and less from the programming side) and Origin (quite extensively). I feel a bit stupid for not knowing some of the above codes (I assume we are discussing botanical or zoological terms here), so let me add that I at least speak some Fortran.

Marco Antonio Pederzini Pereira

As a Mechanical Engineering student, I was looking for some demonstrations that are related to this field of technology. I was surprised by that resourceful Helicopter Tilt Rotor demonstration, it makes me wonder a lot of possibilities that Mathematica can provide if you know how to use its tools.

I’m a Brazilian student of Mechanical Engineering. I came to Hungary to study one year at Budapest University of Technology and Economics by a Brazilian Government program called “Science Without Borders”. My experience with these kind of softwares is pretty scarce. I know just the basics C/C++ programming language, and I had a short course about MatLab. It was on that course that I realized how these programs can be such a powerful tools.

Tamás Pölöskei

Bence Tipary

András Vértesaljai

Bence Bogdola

Dávid Horváth

Tamás Mezei

Tamás Pölöskei

Bence Tipary

Beginners

1. Investigate the function x↦5(e^(-3x))-4(e^(-2x)) using Plot, D, Solve

Bence Bogdola

In this section I’m going to investigate the function x↦5(e^(-3x))-4(e^(-2x)) using Plot, D, Solve.

Plot[y[x]=5(E^(-3x))-4(E^(-2x)),{x,-500,1000}, PlotTheme->"Detailed",PlotStyle->Thick]

Plot[y[x]=5(E^(-3x))-4(E^(-2x)),{x,-5,10}, PlotTheme->"Classic"]

Solve[5(E^(-3x))-4(E^(-2x))==0,x]

Solve[5(E^(-3x))-4(E^(-2x))==0,x,Reals]

Since x↦5(e^(-3x))-4(e^(-2x)) is not a differential equation, it is not possible to use DSolve.

Carlos Henrique Cazagrande

Dávid Horváth (Neatbooks, but physical formatting)

ClearAll[f]
f[x_]=5 Exp[-3x]-4 Exp[-2 x]

Plot [f[x],{x, -1,5}, GridLines->Automatic]

Zero of the function:

z=x/.Solve[f[x]==0,x, Reals]

Simplify[%]

Log[a b]/.Log[x_ y_]->Log[x]+Log[y]

Derivative:

g=D [f[x],x]

gg[x_]:=D [f[x],x]

ff[x]=(x^2)

ff[4]

ff[y]

ff[x]

minx=Solve[g==0,x, Reals]

Simplify[%]

D[f[x],{x,2}]/.minx

Simplify[%]

N[%% ]

Thus x_min=ln(15/8) is a global minimum.

Tamás Mezei

Carsten Olm

BaseFct = 5((E )^(-3x)) - 4(E^(-2x))

Plot[BaseFct,{x,-0.2,1}]

D[BaseFct,x]

Manipulate[D[BaseFct,{x,order}],{order,1,100,1}]

Solve[5((E )^(-3x)) - 4(E^(-2x))==1,x,Reals]

Solve[5((E )^(-3x)) - 4(E^(-2x))==0,x,Reals]

Marco Antonio Pederzini Pereira

f[x]==5E^(-3x)-4E^(-2x)

Tamás Pölöskei

Clear[f]
f[x_]:=5*(E^(-3x))-4(E^(-2x))

FunctionDomain[f[x],x]

True what?! The whole

FunctionRange[f[x],x,y]//N

Plotting the function

Plot[f[x],{x,0,10},PlotRange->All,PlotTheme->"Scientific"]

Finding the abscissae of the possible local extremum

sol1=x/.(Solve[D[f[x],x]==0&&x∈Reals,x]//Flatten)//N

D[f[x],{x,2}]/.{x->sol1}

f[sol1]

It is indeed a local extrema, moreover it is a local minima.

Bence Tipary

The given function

f[x_]:=5(E^(-3x))-4(E^(-2x))

Plotting the function

Plot[f[x],{x,-1,5},PlotRange->{-1,5},PlotStyle->{Red,Thick}]

The first and second derivative of the function

df[x_]:=D[f[x],x]
df[x]
d2f[x_]:=D[f[x],{x,2}]
d2f[x]

Zero crossings of the function

sol1=Solve[f[x]==0,x,Reals]
%//N

Location of the minima and maxima

sol2=Solve[df[x]==0,x,Reals]
%//N

Sign changing of the first derivative

Reduce[df[x]<0,x,Reals] (*strictly monotonously decreasing*)
%//N
Reduce[df[x]>0,x,Reals](*strictly monotonously increasing*)
%//N

Minimum value of the function

f[x]/.sol2
%//Simplify
%//N

Minimize[f[x],x]

Simplify[%]

Location of the inflexion points

sol3=Solve[d2f[x]==0,x,Reals]
%//N

Sign changing of the second derivative

Reduce[d2f[x]<0,x,Reals](*concave*)
%//N
Reduce[d2f[x]>0,x,Reals](*convex*)
%//N

Value of the function at infinity

f[∞]

Tamás Török

f[x_]=5(E^(-3x))-4(E^(-2x))

Plot[f[x],{x,0,3}]

The curve’s zeroplace is:

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

N[%]

Local minimum’s place is:

d=f'[x]

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

Simplify[zerus]

N[%]

I don’t understand why is it the same as zeroplace

lok not zerus

Derivation check:

Integrate[d,x]

András Vértesaljai

ClearAll[f]
f[x_]:=5E^(-3x)-4E^(-2x);
f[x]
Plot[f[x],{x,-2,2}]
fx0=x/.N[Solve[f[x]==0,x,Reals]];
fx0[[1]] "     /at this value f(x)=0"
"1. derivative:" f'[x]
Plot[f'[x], {x,-2,2}]
fdx0=x/.N[Solve[f'[x]==0,x,Reals]];
fdx0[[1]]  "     /at this value f'(x)=0"
f[fdx0[[1]]]"     /minimum value of f(x)"   
"2. derivative:" f''[x]
Plot[f''[x],{x,-2,2}]
fddx0=x/.N[Solve[f''[x]==0,x,Reals]];
fddx0[[1]]  "     /at this value f''(x)=0"

Limit[f[x],x->Infinity]

Limit[f[x],x->-Infinity]

FunctionDomain[f[x],x]

Xuan

f[x] = 5(E^(-3x)) -4(E^(-2x))

Plot[5 (E^(-3 x))-4 (E^(-2 x)),{x,-1.4216483149816193`,0.22280239262433`}]

D[f[x],x]

Plot[-15 (E^(-3 x))+8 (E^(-2 x)),{x,-1.015943168646471`,0.6285075389594781`}]

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

2. Calculate the area under the standard parabola in the interval [0,1]. exactly. (Advanced: approximately.)

Bence Bogdola

In this section I am going to calculate the area under the standard parabola in the interval [0,1].

The function:

fv[x_]:=(x^2)

Exact calculation:

(∫_0^1)fv[x]\[DifferentialD]x

Carlos Henrique Cazagrande

Dávid Horváth

Integrate[x^2, {x,0,1}]

Integrate[a x^2+b x+c, {x,0,1}]

NIntegrate[x^2, {x,0,1}]//Timing

N[Integrate[x^2, {x,0,1}]]//Timing

Szum[N_, a_, b_, c_]:=(1/N )*Sum[a(i/N)^2+b (i/N)+c, {i,1,N}]

Szum2[N_, a_:1, b_:0, c_:0]:=(1/N )*Sum[a(i/N)^2+b (i/N)+c, {i,1,N}]

Szum2[12]

Szum2[12,3,5]

Szum[100, a,b,c]

N[%]

%/.{a->1,b->0,c->0}

Tamás Mezei

Carsten Olm

Integrate[(x^2),{x,0,1}]

(1/3)

This is how it looks like (filled area) :

Plot[{(x^2),If[0≤x≤1,0]}, {x,-1,1},Filling->{1->{2}}]

Marco Antonio Pederzini Pereira

Tamás Pölöskei

Exact solution

ClearAll[f]
f[x_]:=(x^2)

Integrate[f[x],{x,0,1}]

Numerical integration by the rectangle rule

Start and the end of the interval

xSTART=0;
xEND=1;

For one specific subinterval number

Number of subintervals

Nsub=10;

Size of the subintervals

Δx=(xEND-xSTART)/Nsub;

RES=0;

For[
i=1,
i≤ Nsub,
i++,
RES=Δx f[((i+(i-1))/2) Δx]+RES
]
RES//N

Examining the effect of the number of subintervals

Nsubmax=20;

RES=Array[0&,Nsubmax];
For[
Nsub=1,
Nsub≤Nsubmax,
Nsub++,
Δx=(xEND-xSTART)/Nsub;
For[
i=1,
i≤ Nsub,
i++,
RES[[Nsub]]=Δx f[((i+(i-1))/2) Δx]+RES[[Nsub]]
]
]
ListLinePlot[RES,PlotRange->All,PlotMarkers->Automatic,Epilog->{Black,Thick,Dotted,Line[{{0,1/3},{Nsubmax,1/3}}]}]

Numerical integration by the trapezoidal rule

Start and the end of the interval

xSTART=0;
xEND=1;

For one specific subinterval number

Number of subintervals

Nsub=10;

Size of the subintervals

Δx=(xEND-xSTART)/Nsub;

RES=0;

For[
i=1,
i≤ Nsub,
i++,
RES=Δx*((f[i Δx]+f[(i-1) Δx])/2)+RES
]
RES//N

Examining the effect of the number of subintervals

Nsubmax=20;

RES=Array[0&,Nsubmax];
For[
Nsub=1,
Nsub≤Nsubmax,
Nsub++,
Δx=(xEND-xSTART)/Nsub;
For[
i=1,
i≤ Nsub,
i++,
RES[[Nsub]]=Δx ((f[i Δx]+f[(i-1) Δx])/2)+RES[[Nsub]]
]
]
ListLinePlot[RES,PlotRange->All,PlotMarkers->Automatic,Epilog->{Black,Thick,Dotted,Line[{{0,1/3},{Nsubmax,1/3}}]}]

NIntegrate[f[x],{x,0,1},Method->"TrapezoidalRule",MaxPoints->4]

NIntegrate[f[x],{x,0,1},Method->{"TrapezoidalRule","RombergQuadrature"->False}]

NIntegrate[f[x],{x,0,1},Method->{"RiemannRule","Type"->"Left"},PrecisionGoal->1]

Bence Tipary

Standard parabola

g[x_]:=(x^2)

Lower and upper boundary

lb=0;
ub=1;

The curve and the area which has to be calculated

filled=Plot[g[x],{x,lb,ub},Filling->Bottom,PlotStyle->None,FillingStyle->Gray];
curve=Plot[g[x],{x,-0.5+lb,0.5+ub},PlotStyle->{Black,Thick}];
Show[filled,curve,PlotRange->{{-0.5+lb,0.5+ub},{-0.5,0.5+(ub^2)}}]

Exact calculation by definite integral

Integrate[g[x],{x,lb,ub}]

The indefinite integral

Integrate[g[x],x]

After substitution (check)

Integrate[g[x],x]/.x->1-Integrate[g[x],x]/.x->0

Calculation of the area approximately using Rieman Sum

imgsize=350; (*increase to enlarge figures*)
nmaxmax=100;(*increase to have more rectangles at the upper limit*)
Manipulate[{
Show[{filled,Graphics[{EdgeForm[Thin],Red,Table[Rectangle[{((ub-lb)/nmax)(n-1),0},{((ub-lb)/nmax)n,g[((ub-lb)/nmax)(n-1)]}],{n,1,nmax}]}],curve},ImageSize->imgsize,PlotLabel->"Size of area using the lower value: "~~ ToString[Total[Table[((ub-lb)/nmax) g[((ub-lb)/nmax)(n-1)],{n,1,nmax}]]//N]~~" (e^2)"],
Show[{filled,Graphics[{EdgeForm[Thin],Red,Table[Rectangle[{((ub-lb)/nmax)(n-1),0},{((ub-lb)/nmax)n,g[((ub-lb)/nmax)(n-(1/2))]}],{n,1,nmax}]}],curve},ImageSize->imgsize,PlotLabel->"Size of area using the mean value: "~~ ToString[Total[Table[((ub-lb)/nmax) g[((ub-lb)/nmax)(n-0.5)],{n,1,nmax}]]//N]~~" (e^2)"],Show[{filled,Graphics[{EdgeForm[Thin],Red,Table[Rectangle[{((ub-lb)/nmax)(n-1),0},{((ub-lb)/nmax)n,g[((ub-lb)/nmax)n]}],{n,1,nmax}]}],curve},ImageSize->imgsize,PlotLabel->"Size of area using the greater value: "~~ ToString[Total[Table[((ub-lb)/nmax) g[((ub-lb)/nmax)n],{n,1,nmax}]]//N]~~" (e^2)"]},{nmax,1,nmaxmax,1}]

Tamás Török

2, Area calculate Calculating the area

g[x_]=x^2

Plot[g[x],{x,0,1}]

Integrate[g[x],{x,0,1}]

The area under the cruve is 1/3.

András Vértesaljai

Integrate[x^2,{x,0,1}]

f[x_]:=(x^2)

values[n_]:

values[3]

((values[3])^2)

1/3 %

Total[%]

rupp[n_]:=Total[1/n ((values[n])^2)]

rupp[3]

ruppgen[n_,f_:Function[x,(x^2)]]:=Total[1/n Map[f,Range[1/n,1,1/n]]]

3. Manipulate the plot of BesselJ functions with their first parameter changing. Find and write down the defintions of these functions.

Bence Bogdola

BesselJ gives Bessel functions of the first kind ((Subscript[J, n])(z)), that satisfies the differential equation (z^2)y''+zy'+((z^2)-(n^2))y=0.

Manipulate[Plot[BesselJ[n,z],{z,0,120},PlotRange-> {-0.4,0.4}],{n,0,100,1}]

Carlos Henrique Cazagrande

Dávid Horváth

Manipulate[Plot[BesselJ[n,x],{x,-4*n-10,4*n+10},TicksStyle->Directive[Red, Bold]],{n,-2,10,0.5}]

BesselJ (Subscript[J, n])(z) functions are the solutions of the differential equation (z^2) y''+z y'  + ((z^2)-(n^2))y=0 that are finite at the origin for integer or positive n.  For negative, non integer n these functions diverege as x->0. (Subscript[J, n])(z) can also be define with the following integral: (Subscript[J, n])(z)= (1/π)(∫_0^π)cos(nt-x sin(t))dt.

Tamás Mezei

Carsten Olm

I' m not 100 % sure whether this is what you were asking for. Also, which functions do you mean? Did you want us to show the results of the help function?

Anyway, I think it’s nice looking.

Manipulate[Plot[BesselJ[n,y],{y,0,100}],{n,0,100}]

?? BesselJ

also:

Marco Antonio Pederzini Pereira

Tamás Pölöskei

Definition of the BesselJ[n,z] function

(Subscript[J, n])(z) satisfies the differential equation (z^2) y''+z y'+((z^2)-(n^2))y=0.

Graphical proof of BesselJ function

f[n_,z_]:=(z^2)(D[BesselJ[n,k],{k,2}]/.k->z)+z (D[BesselJ[n,k],k]/.k->z)+((z^2)-(n^2))*BesselJ[n,z]

Almost zero

Manipulate[Plot[f[n,z],{z,0,60},PlotRange->All],{n,0,10}]

Plot of the BesselJ function

Manipulate[Plot[BesselJ[n,x],{x,0,60},PlotStyle->{DotDashed,Thick,Green},PlotRange->All],{n,0,10}]

Bence Tipary

Manipulation of the plot of the BesselJ functions by first parameter

Manipulate[Plot[BesselJ[n,x],{x,0,100},PlotRange->{-1,1}],{n,-10,10,0.25}]

BesselJ functions are Bessel functions of the first kind.  These functions are the solutions of the Bessel’s differential equation. The value of the function is finite at the origin if the parameter is integer or positive, and diverge when x tends to zero and the parameter is negative non integer.

Tamás Török

Manipulate[Plot[BesselJ[a,x],{x,-50,50}],{a,0,10}]

The BesselJ fubction first parameter: 
	If “a” is pair than the curve is axisymmetry for ordinate, but if “a” is unpair than the curve is sectiopnsymmetrical for origo.

András Vértesaljai

N[Manipulate[BesselJ[n,5],{n,1,10,1}],10]

N[BesselJ[2,5]]

You may have a look at functions.wolfram.com.

Xuan

mybes=Plot[BesselJ[n,5], {n,0,5}]

mymanipulate = Manipulate[Plot[BesselJ[n,5.5],{n,0,10}],{n, 0,5}]

Definitions 
BesselJ function (BesselJ[n,z]): is the Bessel function of the first kind J[n,z].

4. Create a table consisting of the first 100 prime numbers and present them in a nice form.

Bence Bogdola

primes:=Table[Prime[n],{n,100}]

TableForm[primes,TableHeadings->{Range[1,100],None}]

Carlos Henrique Cazagrande

Dávid Horváth

TableForm[Table[Prime[i],{i,1,100}], TableHeadings->{Range[1,100], None}, TableDepth->25]

Maybe the following one is more apparent:

Grid[Table[{{i, Prime[i]},{i+25, Prime[i+25]},{i+50, Prime[i+50]},{i+75, Prime[i+75]}},{i,1,25}]]

Tamás Mezei

Carsten Olm

Please scroll right!

TableForm[{Range[1, 100],Prime[Range[1, 100]]},TableHeadings->{{"No.","Value"},None}]

I hope that will do. Please let me know if I misunderstood something or made a mistake.

Together with scrolling I do not think it is as useful :)

Marco Antonio Pederzini Pereira

Tamás Pölöskei

TableForm[Table[{Prime[i],Prime[11+i],Prime[21+i],Prime[31+i],Prime[41+i],Prime[51+i],Prime[61+i],Prime[71+i],Prime[81+i],Prime[91+i]},{i,1,10}],TableHeadings->{Range[10],Range[0,90,10]}]

Partition[Prime[Range[100]],10]

{{2,3,5,7,11,13,17,19,23,29},{31,37,41,43,47,53,59,61,67,71},{73,79,83,89,97,101,103,107,109,113},{127,131,137,139,149,151,157,163,167,173},{179,181,191,193,197,199,211,223,227,229},{233,239,241,251,257,263,269,271,277,281},{283,293,307,311,313,317,331,337,347,349},{353,359,367,373,379,383,389,397,401,409},{419,421,431,433,439,443,449,457,461,463},{467,479,487,491,499,503,509,521,523,541}}

TableForm[%]

Bence Tipary

Labeled[Style[TableForm[Table[Prime[j+10k],{j,1,10},{k,0,9}],TableHeadings->{Range[1,10](*name of rows*),Table[ToString[n]~~"-"~~ToString[n+9],{n,1,91,10}](*names of columns*)}],Background->LightGray ,FontFamily->"Arial Black"],"First hundred primes"]

Tamás Török

TableForm[Table[Prime[Range[1,100]]],TableHeadings->{Range[0,100], None}]

András Vértesaljai

p=Prime[Range[100]]

ListPlot[p]

Xuan

myprime =Prime[Range[1, 100]]

TableForm[myprime,TableHeadings->{Range[0,100], None}]

Advanced students

1. Estimate the value of π using the Monte Carlo method

Bence Bogdola

Carlos Henrique Cazagrande

Dávid Horváth

István Mester

The number points are being used:

n=1500;

pts=Table[RandomReal[{-1,1}],{n},{2}];

Show[ptsPlot=ListPlot[pts,AspectRatio->1,Axes->False],Graphics[{{Opacity[0.1],Rectangle[{-1,-1},{1,1}]},{Red,Opacity[0.2],Disk[{0,0},1]}}]]

ptsInCircle=Table[If[pts[[i,1]]^2+pts[[i,2]]^2<1,1,0],{i,1,n}];

p=Total[ptsInCircle]

The PI is approximately:

PiMonteCarlo=p/ n*4//N

points[n_]:=Table[RandomReal[{-1,1}],{n},{2}];

points[3]

((points[3])^2)

Plus@@@%

Tamás Mezei

Carsten Olm

Marco Antonio Pederzini Pereira

Tamás Pölöskei

Creating the list/matrix containing the coordinates of the random generated points

Npoint=10000;
points=Table[{RandomReal[{-1,1}],RandomReal[{-1,1}]},{i,1,Npoint}];
ListPlot[points,AspectRatio->Automatic]

Decide wether or not the selected point is inside the unit circle

approx=0;
For[
i=1,
i≤Npoint,
i++,
If[
Norm[points[[i,All]]]≤1,
approx=approx+1
]
]
πapprox=4. approx/Npoint

Relative difference in %

Print[100 Abs[πapprox-π]/πapprox," %"]

Bence Tipary

The Monte Carlo method by filling a unit square and its inscribed circle with points with random positions

mmax=10000; (*increase to have more points*)
randtable=Table[{RandomReal[{-1,1}],RandomReal[{-1,1}]},{mmax}]; (*table containing the positions of the points*)
pointtable=Table[{If[Norm[randtable[[m]]]<1,Red,Yellow],Point[{randtable[[m,1]],randtable[[m,2]]}]},{m,1,mmax}]; (*table containing the positions and the color of the points*)
Manipulate[Graphics[{Rectangle[{-1,-1},{1,1}],LightGray,Point[randtable[[1;;m]]],Blue,Thick,Circle[{0,0},{1,1}]}],{m,1,mmax,1}]

Coloring the points inside and outside the circle

Graphics[{Rectangle[{-1,-1},{1,1}],pointtable,Blue,Thick,Circle[{0,0},{1,1}]}]

Counting the yellow and red points

y=Count[pointtable[[All,1]],Yellow]

r=Count[pointtable[[All,1]],Red]

total=y+r

Calculation of π

ratio=((4r)/total)
%//N

Error of the calculation

Pi-ratio//N

Tamás Török

Wikipedia says in four steps about Monte Carlo Method:
	1, “Draw a square on the ground, then inscribe a circle within it.
	2, Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
	3, Count the number of objects inside the circle and the total number of objects.
	4, The ratio of the two counts is an estimate of the ratio of the two areas, which is π/4. Multiply the result by 4 to estimate π.”

1, I “draw” a square from x=-2 to x=2

sqa[x_]=2

Plot[sqa[x],{x,-2,2}]

Then I “draw” a circuit within in:

Solve[x^2+y^2==4,y]

cir[x_]=(Sqrt[4-(x^2)])

Plot[cir[x],{x,-2,2}]

The drawings in 1 plot:

Plot[{cir[x],sqa[x]},{x,-2,2},PlotLegends->"Expressions"](*I don't understand why drawed a parabola for cir[x] *)

I calculate the area of the square:

arsqa=(∫_(-2)^2)sqa[x]\[DifferentialD]x

I calculate the area of the circuit:

arcir=(∫_(-2)^2)cir[x]\[DifferentialD]x

I calculate the ratio of them and multiplied with 4. ( I could estimate the π with a half square and cicuit baceuse the ratio is equal as the hole square and circuit ratio).

pi=(arcir/arsqa)*4

András Vértesaljai

ciklus=50;
szorzo=1000;
t=Table[0,{ciklus}];
For[a=1,a<(ciklus+1),a++,
k=0;
n=szorzo*a;
For[i=1,i<(n+1),i++,
v={RandomReal[],RandomReal[]};
If[ v.v<=1, k=k+1]];
t[[a]]=(k*4/n)];
t;
ListPlot[t ,PlotRange->{{0,ciklus+1},{3,3.2}}]

2. Write a program to find all the prime numbers between 1 and 100.

Bence Bogdola

numbers1to100:=Range[1,100]

ClearAll[primes1to100]
primes1to100:={};

For[number=1,number<101,number++,If[PrimeQ[numbers1to100[[number]]],AppendTo[primes1to100,numbers1to100[[number]]],None]]

TableForm[primes1to100, TableHeadings -> {Range[1, 100], None},Tab]

Carlos Henrique Cazagrande

Dávid Horváth

István Mester

The domain from 1 to n:

n=100;

num=Table[i,{i,1,n}];

Do[switch=0;
If[num[[j]]≠0,
Do[If[switch<2,If[Mod[num[[j]],num[[i]]]==0,switch++]],{i,1,j-1}]];
If[switch==1,Do[num[[k]]=0,{k,2*j,n,j}]],
{j,2,n}]//Quiet

primes=DeleteCases[num,0];

The prime numbers from 1 to N are:

TableForm[primes[[2;;]],TableHeadings->{Range[1,Length[num]],None}]

Unfortunately this code is computationally intensive on larger domains (> 10000).

Tamás Mezei

Carsten Olm

Marco Antonio Pederzini Pereira

Tamás Pölöskei

prime=Array[0&,50];

Start the checking algorithm from the number 2, because the number 1 gives a false result

If the selected integer has two divisors, then it is a prime number.

n=2;
num=1;
While[n≤100,
div=0;
For[i=1,
i≤n,
i++,
If[
Mod[n,i]== 0,
div=div+1
]
];
If[
div==2,
prime[[num]]=n;
num=num+1;
]
n++
]
prime

(* Length[Select[prime,#≠0&]] *)

Bence Tipary

Definining a table consists of the first hundred integer

primes=Table[p,{p,1,100}];

By examining divisibility deleting the non-prime numbers

Do[If[Divisible[primes[[q]],p]&&primes[[q]]≠p||primes[[q]]==1,primes[[q]]=0,None],{q,1,100},{p,2,100}] (*replacing every non-prime numbers with 0 to not lose the order*)
primes=Delete[primes,Position[primes,0]] (*deleting the zero values from the list*)

Check

primes==Table[Prime[p],{p,1,25}]

Tamás Török

For[a=2,a<100,a++,If[Prime[a]<100,Print[Prime[a]]]]

András Vértesaljai

3. List of names from EXCEL files

Bence Bogdola

In this section I will explain my solution of changing the name order of names imported from a .xls document.

The very first step is to open the .xls document with the names in it (in the wrong name order) and add a column after the names filling each row with a comma except the last row, as seen below.

After this step the two columns can be selected and copied into the list called LIST. After that one can evaluate the notebook and select the last output of it containing all the names in the correct (reversed) name order. This can be then copied back into the .xls document.

LIST:=List[Szabó István,Kiss Géza,Vass József,Kiss Virág,Darabos Áron,Vallató Kázmér,Tóth Elvira,Kovács Benő,Kosaras Pál,Őri Örs,Tatai Zoltán,Sváb István,Kiss István
]

listlength:=Length[LIST]

ClearAll[NewNameOrder]
NewNameOrder:={};

For[name=1,name≤listlength,name++,AppendTo[NewNameOrder,SplitBy[LIST[[name]]," "]]]

Text[TableForm[NewNameOrder]]

Note: this problem can be solved in a much easier way within Microsoft Excel by dividing cells at the space, then dragging the new column before the first reversing the name order, and then merging the cells in a new column.

Carlos Henrique Cazagrande

Dávid Horváth

István Mester

namesInXLSX = SystemDialogInput["FileOpen", WindowTitle -> "Válassza ki a táblázatot"];

names=Import[namesInXLSX][[1]];
names//TableForm

namesRev=Table[{names[[i,2]],names[[i,1]]},{i,1,Length[names]}];
namesRev//TableForm

Tamás Mezei

Carsten Olm

Marco Antonio Pederzini Pereira

Tamás Pölöskei

SetDirectory[NotebookDirectory[]]

It was easier to use the function Flatten during the problem solving.

Name01=Flatten[Import["Name_01_Tamas_Poloskei.xlsx"],1]

Name02=Array[0&,Dimensions[Name01]];
Name02[[All,1]]=Name01[[All,2]];
Name02[[All,2]]=Name01[[All,1]];

Export["Name_02_Tamas_Poloskei.xlsx",Name02]

Bence Tipary

Importing the names (remark: you have to put the names.xlsx file directly to D:\\ drive, or modify the source location in the notebook, also you will get your newnames.xlsx file on the D:\\ drive)

names=Import["D:\\names.xlsx"][[1]] (*modify here the source location if necessary*)
%//TableForm

Number of rows in the excel file

lmax=Length[names] (*contains the header as well*)

Changing order

newnames={names[[1]]} (*fixing the header*)
Do[newnames=Join[newnames,{{names[[l,2]],names[[l,1]]}}],{l,2,lmax}]
newnames//TableForm

Exporting the new file

Export["D:\\newnames.xlsx",newnames,"XLSX"](*modify here the target location if necessary*)

András Vértesaljai

Homeworks (deadline: Friday evening)

We are going to return to some of the homeworks for which we had not enough time recently.

Immediate help:

Fundamental calculations

Algebra

A polynomial

poly=(1+x)(1+x+(x^2)+(x^3))

Expand[poly]

Factor[poly]

sols=Solve[poly==0]

poly/.x->2

poly

poly/.sols[[1]]

Coefficient[poly,(x^3)]

CoefficientList[poly,x]

MonomialList[poly]

Simplify[poly]

FullSimplify[poly]

Rational functions

quot=Together[1/(1+x)+2/(1-x)]

Apart[quot]

Numerator[quot]

Denominator[quot]

Solving equations

Solve[{x+y==2,x y+2==0},{x,y}]

All real numbers are solutions

Solve[(x^2)+2x+1==(((x+1))^2),x]

There is no solution

Solve[{x+2==1,x≠-1},x]

NSolve[{x+y==2,x y+2==0},{x,y}]

FindRoot[x-Cos[x]==0,{x,0}]

Reduce[a (x^2)+b x+c==0,x]

Reduce[{a+b+c==0,a b+a c+b c==0,a b c==1},{a,b,c}]//LogicalExpand

Calculus

expr=2+3x+x^2-Log[x];

d1=D[expr,x]

d2=D[%,x]

Integrate[d2,x]

Integrate[%,x]

Integrate[expr,x]

Integrate[expr,{x,1,2}]

Integrate[(x/(Log[x])),x]

Numerical integration

NIntegrate[(x/(Log[x])),{x,2,3}]

N[Integrate[(x/(Log[x])),{x,2,3}]]

NIntegrate[(x/(Log[x])),{x,2,3},WorkingPrecision->25,AccuracyGoal->20]

Options[NIntegrate]

Improper integrals

Integrate[(1/(x^2)), {x, 1, Infinity}]

Complicated problems need time

NIntegrate[Boole[1/4≤(x^2)+(((2y))^2)≤1∧y>x],{x,-1,1},{y,-1,1}]

RegionPlot[1/4≤(x^2)+(((2y))^2)≤1∧y>x,{x,-1,1},{y,-1,1},PlotPoints->200]

ℛ=ConvexHullMesh[RandomReal[1,{50,3}]]

NIntegrate[x^2y^2z^2,{x,y,z}∈ℛ]

NIntegrate[(Sqrt[(1/(Cos[5x]Sin[5y]-1/2))]),{x,0,5},{y,0,5},Exclusions->Cos[5x]Sin[5y]==1/2]//Quiet

Plot3D[{Re[(Sqrt[(1/(Cos[5x]Sin[5y]-1/2))])],Im[(Sqrt[(1/(Cos[5x]Sin[5y]-1/2))])]},{x,0,5},{y,0,5},Exclusions->Cos[5x]Sin[5y]==1/2,Mesh->None,PlotStyle->{Yellow,Magenta},PlotPoints->35]

Differential equations

A deriváltat vesszővel jelöljük. A C egy védett függvény, a C[1] az első szabad konstans.

DSolve[y'[x] + y[x] == 1, y[x], x]

DSolve[y'[x] + y[x] == 1, y, x]

DSolveValue[y'[x] + y[x] == 1, y, x]

DSolveValue[y'[x] + y[x] == 1, y[x], x]

NDSolve[{y'[x] == y[x], y[0] == 1}, y, {x,0,2}]

rhs1={(k1-k2 s)r,(k1-k2 r)s};
param1={k1->1,k2->1};StreamPlot[rhs1/.param1,{r,0,3},{s,0,3}]

NDSolve[{D[u[t,x,y],{t,2}]==D[u[t,x,y],{x,2}]+D[u[t,x,y],{y,2}]/2+(1-((u[t,x,y])^2))(1+2u[t,x,y]),u[0,x,y]==(E^(-((x^2)+(y^2)))),u[t,-5,y]==u[t,5,y],u[t,x,-5]==u[t,x,5],Derivative[1,0,0][u][0,x,y]==0},u,{t,0,4},{x,-5,5},{y,-5,5}]

Table[Plot3D[Evaluate[u[t,x,y]/.%],{x,-5,5},{y,-5,5},PlotRange->All],{t,1,4}]

Limit and extrema

Limit[Sin[x]/x,  x->0]

IntegerString[1,"Roman"]

Limit[1/x, x->0, Direction-> 1]

Limit[1/x, x->0, Direction-> -1]

Minimize[(Sqrt[(x^2)+((((x^2)+1))^2)]),x]

Minimize[{(Sqrt[(x^2)+((((x^2)+1))^2)]),1<x<2},x]

Linear algebra

m={{1,2,3},{4,3,6},{-3,4,5}}

m//MatrixForm

Det[m]

{Eigenvalues[m],Eigenvectors[m]}

Tr[m]

m m

m.m

MatrixPower[m,4]//MatrixForm

Trace[] is something different...

Import

SetDirectory[NotebookDirectory[]]

res=Import["MathBrasil.xlsx"]

Import["MathBrasil.xlsx","Elements"]

res[[1]]//TableForm

Everybody

0. Read, modify, use and understand the solutions of the problems given by others. If you do not understand something not even using the help, ask the author or ask me.

Newcomers should solve the first two problems.

1. Find a nice application in demonstrations.wolfram.com, and write down why you like it.

2. Write a personal introduction into a Mathematica notebook. (Your studies, your experience with either Mathematica, or Maple, Matlab, Origine, Derive, R, Sage, Spyder etc.) If the program is not availabel yet, use TEX, Word or whatever.

3. Consult functions.wolfram.com about the trigonometric functions, like sin (or Bessel functions, if you are interested). Cite a statement about the trigonometric functions that  is new for you.

4. Show one example for each group in the Fundamental calculations. Use the options of the functions.

Advanced problems

5. Give a neat (streamlined) solution for the numerical integration problem (choose any method).

6. Find a data file in EXCEL form on the net or use your own data being collected in an EXCEL file, Import it, and create a nice table using the options of Import (without string manipulations).

7. Write a program to apply Eratosthenes’ sieve for obtaining primes between 1 and 100.