Scientific & Engineering Programming

Lecture V

Diversitas Mathematica

Robert Muszyński

Department of Cybernetics and Robotics
Wrocław University of Science and Technology

Copyright 2017-2019 RM

Work methodology

READ WARNINGS AND ERROR MESSAGES

DON’T IGNORE THEM

Build expressions using bottom-up strategy

Watch for colors: black - okey, blue - hmm...

Examine expressions components when the computation fails

Use two notebooks with one common kernel for debugging

Work methodology

Working example: double_pendulum_control_full.nb

misspelled variables (e.g. (Subscript[l, 12]) instead of (Subscript[l, 1]) in params variable) - NDSolve doesn’t work

examine NDSolve arguments:  eqns, eqns/.params, ics, ics/.params

substitute state vectors, their derivatives with zeros, ones

sub=Apply[Rule,{vq[t],{0,0}}//Transpose,1]

non numerical data in plots (e.g. (a,0) instead of (0,0) in pendul definition)

examine plots components: expressions, lines, other graphical elements

substitute independent variable (time) with zero, one

/.t->0

wrong dimensions of vectors, matrices (e.g. mB.(mQ^(-1)) instead of (mQ^(-1)).mB in mP definition)

examine parts of the expression in doubt

substitute symbolic elements with specific numeric values

wrong input data (e.g. desired trajectory vyd[t] with a instead of 0.3) - NDSolve doesn’t work again

refer to the first case

More graphics

Manipulate with locator

Let’s solve something

ode1={y''[t]+Sin[y[t]]==0,y[0]==2,y'[0]==1};
sol=NDSolve[ode1,y,{t,0,20}]//First
(* time plots *)
Plot[y[t]/.sol,{t,0,20}]
Plot[Evaluate[{y[t],y'[t],y''[t]}/.sol],{t,0,20}]

The same in the phase plane

ParametricPlot[Evaluate[{y[t],y'[t]}/.sol],{t,0,20}]

The same interactively

Manipulate[Module[{sol=NDSolve[{y''[t]+Sin[y[t]]==0,y[0]==p[[1]],y'[0]==p[[2]]},y,{t,0,T}]},ParametricPlot[Evaluate[{y[t],y'[t]}/.sol],{t,0,T},PlotRange->{{-10,10},{-4,4}}]],{{p,{2,1}},Locator},{{T,10,"time"},0.1,30}]

Something more sophisticated

splot=StreamPlot[{y,(1-x^2) y-x},{x,-4,4},{y,-3,3},StreamColorFunction->"Rainbow"];
Manipulate[Show[splot,ParametricPlot[Evaluate[First[{x[t],y[t]}/.NDSolve[{x'[t]==y[t],y'[t]==y[t] (1-x[t]^2)-x[t]+0.5 Cos[1.1 t],Thread[{x[0],y[0]}==point]},{x,y},{t,0,T}]]],{t,0,T},PlotStyle->Red]],{{T,20,"time"},1,100},{{point,{3,0}},Locator},SaveDefinitions->True]

...and colorful (and time consuming :)

splot=LineIntegralConvolutionPlot[{{y,(1-x^2) y-x},{"noise",1000,1000}},{x,-4,4},{y,-3,3},ColorFunction->"BeachColors",LightingAngle->0,LineIntegralConvolutionScale->3,Frame->False];
Manipulate[Show[splot,ParametricPlot[Evaluate[First[{x[t],y[t]}/.NDSolve[{x'[t]==y[t],y'[t]==y[t] (1-x[t]^2)-x[t]+0.5 Cos[1.1 t],Thread[{x[0],y[0]}==point]},{x,y},{t,0,T}]]],{t,0,T},PlotStyle->White]],{{T,20,"time"},1,100},{{point,{3,0}},Locator},SaveDefinitions->True]

More graphics

AnatomyPlot3D[{Entity["AnatomicalStructure", "Skeleton"], Entity["AnatomicalStructure", "AlimentarySystem"]}, PlotRange -> Entity["AnatomicalStructure", "Trunk"], ImageSize -> 300]

More on Wolfram page
https://www.wolfram.com/language/11/knowledgebase-expansion/visualize-and-analyze-anatomical-structures.html

Neural Networks

Digit classification

Obtain the training and validation data

resource = ResourceObject["MNIST"]; trainingData = ResourceData[resource, "TrainingData"]; testData = ResourceData[resource, "TestData"];

RandomSample[trainingData, 5]

Define a convolutional neural network that takes in 28×28 grayscale images as input

lenet = NetChain[ {ConvolutionLayer[20, 5], Ramp, PoolingLayer[2, 2], ConvolutionLayer[50, 5], Ramp, PoolingLayer[2, 2], FlattenLayer[], 500, Ramp, 10, SoftmaxLayer[]}, "Output" -> NetDecoder[{"Class", Range[0, 9]}], "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}] ]

Train the network

lenet = NetTrain[lenet, trainingData, ValidationSet -> testData, MaxTrainingRounds -> 3];

Evaluate the trained network directly on images randomly sampled from the validation set

imgs = Keys @ RandomSample[testData, 5]; Thread[imgs -> lenet[imgs]]

Refer to the second Neat Example for  Classify function (logistic classifier for digits - live handwrite digits recognition)

?Classify

Social media analysis

ServiceConnect["Flickr"]

Not Connected?  Guess why?  ;)

so go to the exemplary Wolfram page
https://www.wolfram.com/language/11/computational-photography/import-and-analyze-a-collection-from-social-media.html

Let’s travel around

We are here

wroclaw=Entity["City",{"Wroclaw","Dolnoslaskie","Poland"}]
poland=Entity["Country","Poland"]

And there is us...

EntityValue[wroclaw,"Population"]

and was...

EntityValue[wroclaw,EntityProperty["City","Population",{"Date"->DateObject[{2005}]}]]

lives=Map[EntityValue[wroclaw,EntityProperty["City","Population",{"Date"->DateObject[{#}]}]]&,Range[1975,2017]]

ListPlot[lives]

What else?

EntityProperties[wroclaw]

How to enter all this stuff?

Date objects

DateObject[]
DateObject[]-DateObject[{1970,1,1}]
AbsoluteTime[DateObject[]]-AbsoluteTime[DateObject[{1970}]]

DateObject[AbsoluteTime[DateObject[{1970}]]+1.5 (10^9)]

Entities

A list of Entities types

EntityValue[]

Use Free Form Input (equal sign)

For more go to the help ref/MovieData

Back to Poland

Length[cities=CityData[{Large,"Poland"}]]

roads=Quiet@TravelDirections[{#,wroclaw},"TravelPath"]&/@cities;

Length[roads=Cases[roads,_GeoPath]]

GeoGraphics[{GeoStyling[White],Polygon[poland],Red,Opacity[.1],roads,Black,Opacity[0.6],Point[cities],Blue,Opacity[1],PointSize[Large],Point[wroclaw]},GeoBackground->LightBlue,ImageSize->600,GeoRange->poland]//Rasterize