# Lab session on week 12 (8th meeting) 1 more week to go…

## Presentation on theme: "Lab session on week 12 (8th meeting) 1 more week to go…"— Presentation transcript:

Lab session on week 12 (8th meeting) 1 more week to go…
IT1005 We will take class photo today! At the end of the 1st hour of this session…

This Week’s Agenda Discussion of my Term assignment answers. Review on
I believe that most of you have seen the same tables (for q1), and the same plots (for q2-3), but do you curious to know what are my (Lab TA) answers compared with yours?  Note, my answers are not 100% correct. This is not official answer! Review on Various ODE IVP solvers, Curve Fitting using Matlab, and The initial part of Optimization using Matlab Overview of Lab 8, our last lab Preview of what is in store for the last week… Class photo :D

Term Assignment – Q1.A Q1. A. Naïve trapezium rule, definitely inaccurate. (b-a)*(f(a)+f(b))/2, f(a) is (f(0)), f(b) is f(1000), the result is very imprecise. Let’s observe the plot of f(t) from 0 <= t <= 1000  Parameter c controls the ‘curvature’ of f(t). Only if c = then naïve trapezium rule still give a good approximation. When c is larger, the ‘error’ around this area is bigger Wrong if you draw a triangle here! Note that the y axis start from 0.5! We should draw a trapezium 

Term Assignment – Q1.B Q1. B. More precise trapezium rule, larger n should be ‘more precise’. But why all n produces similar values between Matlab ‘quad’ (likely correct) and my traprule2 (likely not ok)? You get no error because the tested integral range is from -10 to 10 and the graph is ‘special’ (symmetrical) (positive-negative errors cancel each other). That is how you should explain why there is no error. If you change the range of your integral, you will see differences! If you do this, you can show that n = 2 is definitely imprecise. The error from larger/slower n (where it should logically be more precise) is likely because of floating point error around t = 0. Larger n is still better (and slower) for most cases. You can also mention that there is no major effect of increasing n when t is far from 0 for this f(t) with high c.

Term Assignment – Q2.A-B Q2. A. IVP, IVs = [0.1; 0.1] Q2. B. Interpretation of the plot: the population change periodically (roughly every 5 years), population curve of the predator (lion) is steeper and trails the curve of prey (zebra) by a bit. Bla bla (tell some nice story)… Tricky question: which ODE solver should I use for this case? No major difference between ode45 and ode15s for 10 years… Pick the faster solver then… ode15s? Note: I initially thought ode45 is faster… Some students from Friday groups mentioned that ode15s is actually faster…

Term Assignment – Q2.C-D Q2. C. To find steady state solutions, use fsolve or solve (symbolic math), Steady state means both dx/dt = 0 and dy/dt = 0 We have two possible steady states: [x = 0.5, y = 1/3] or [x = 0, y = 0] (both extinct) Q2. D. If you supply any of the two steady state IVs, lion/zebra population will be unchanged over time. The [x = 0, y = 0] can be ignored since it is very trivial. NOTE: for [x = 0.5, y = 1/3], if you write 0.33, the graph may looks curvy (this is imprecise)!

Term Assignment – Q2.E Q2. E. If population of zebra versus lion is plotted, what will we see? A circle (oval/ellipse/egg-shape to be precise)… but which ODE solver that we should use this time? In predator prey relationship, predator depends on the prey to survive. If both has non zero population, their population count will always be on the border of this ellipse. This one looks growing perhaps because of the constants a, b, c, d used in the question. Intermezzo: If prey extinct, predator also extinct soon (no food), but if predator extinct first, prey can survive and even fill the earth (no predator :D), but this model is inaccurate as we omit the death factor of the prey (look at problem description again). Can use different color to show the ‘growth’ ode45 left (steady). ode15s right (growing). Is it because ode15s more precise or what? I think ode15s is better

Term Assignment – Q3.A Q3. A. IVP again, but now 4 ODEs, create a loop to compute the dx/dt. Looking at the differential equation, ri is the birth rate, α is interaction matrix, we know that: Species 1 has average birth (1), but is preyed by/interact with species 2 & 3 (total -2.61) Species 2 has least birth (0.72), but is preyed by/interact with species 3 & 4 (total -1.8), Species 3 has most birth (1.43), but is preyed by/interact with species 1 & 4 (total -2.8), Species 4 has 2nd most birth (1.27), but is preyed by/interact with species 1, 2 & 4 (total -2.07), Looking at the interaction matrix and the plot: Species 1 is likely predator of Species 3 and 4. Species 2 is likely predator of Species 1. Note that there are other ways to describe this plot, e.g. several species competing for the same resource, etc… If you forgot to change r(3) from 1.53 to 1.43, you will see straight lines around here

Term Assignment – Q3.B-C Q3. B. 3-D plot for 3 species, 3-D plot plus 4th axis is color if you want to show 4 species. If these 4 species are alive, their population count will fall on the surface of this mesh. Q3. C. Lotka-Volterra, a study of population dynamics! Open ended… Hard to grade this one…

Application 5: IVP Solvers
Matlab has several ODE IVP solvers Which one to use? Generally, ode45, ok for normal cases But if the ODEs are stiff, use the more precise solver ode15s Intermezzo, what do you think the plot of “number of term assignment submitted by students over time” will looks like? A stiff plot :p, perhaps something like this.

Application 6: Curve Fitting
This is a graph for my/our research paper (due Friday, 11 Apr 08, 6pm) Look at how I use polyfit/polyval.

Application 7: Optimization
Hm…, fyi, my research is about optimization too. ‘A little bit’ more complex than what you see in your lecture notes, but the idea is the same: given so many options, choose one option that minimizes (or maximizes) some objective function. e.g. I deal with problems like TSP (Traveling Salesman Problem), n cities, n! possible tours, one of the tour is shortest. How to deal with simple optimization in Matlab Code the function that you want to optimize in a dedicated function, similar like what we have done with fsolve and ODE solver. Call [x, f] = initial_guess); Wrong initial guess will bring us to ‘local optima’ (good, but not the best). Next: fmincon Constraints ‘redefine’ the search space, the global optima may be different than the unconstrained one.

Lab 8 - Overview No ODE (already tested in term assignment), Lab 8 tests fzero (the leftover from Lab 6), polyfit (Curve fitting), and fminsearch (Optimization), in fact, fminsearch is used to do curve fitting. Q1. A-B. fzero, this is the former Lab 6 question 3. If you have not tried this before, do this now! PS: I have taken out my answers from my ppt slides 2 weeks ago. Q1. C. Use polyfit to the results in Q1.B above, and plot the graph. Q2. A. Create a function to compute sum of squared errors. Q2. B. Use fminsearch to find k1, k2, and k3 that minimizes the error in Q2. A and plot the fit. Should be straightforward, you do not need to wait until week 13 to finish this.

What is left for next week?

Class Photo (1) I always do this every semester
You can download our photos here: Photos from my Wednesday classes: 2B B B

Class Photo (2) Photos from my Friday classes: 5A A

Free and Easy Time Now, you are free to explore Matlab to:
Do your Lab 8! Review the lessons that you have received during IT1005 course!

Similar presentations