# Lab 4Field plots in 2d(Some of this will be covered on your Math 340 Midterm, keep this and use it to study. Dr. Pacheco)Example 2.2.1The fields now are 2d. When we plot in 2d, by necesity we ignore time.

Lab 4Field plots in 2d(Some of this will be covered on your Math 340 Midterm, keep this and use it to study. Dr. Pacheco)Example 2.2.1The fields now are 2d. When we plot in 2d, by necesity we ignore time. If we want to do system in time plots, we have to go 3d (later). Notice that most of the plots, even of solutions, will be parametric [x(t) y(t)] plots, rather than individual plots of x and y in the same function plot.df1 = @(t, x, v) {v; -x}; % defined with t for later use in odeXX. t not used % we know the solution for this equation (will show you later) A = 1; B = 2; f1 = @(t) [A*cos(t(:))+B*sin(t(:)), -A*sin(t(:))+B*cos(t(:))]; f1 is the known solution for x and v, as a vector. Here is what it looks like, in a form that you would easily recognize. You can read off the initial conditions for this system from the first point.fplot(f1, [0,7]) xlabel(‘t’) ylabel(‘x, v’) legend({‘x’,’v’}) title(‘Figure 1’)Now, we want to look at the field, and solution, in the state space, that is now 2d. Our friend quiver still works:[xs, vs]=meshgrid(-3:.5:3,-3:.5:3); %[dx,dv]=df1(xs,vs) % that how I’d wish would happen, % but matlab is peculiar and constrained with @ functions out=df1(0,xs,vs); [dx,dv]=out{:}; quiver(xs,vs,dx,dv) axis square xlabel(‘x’) ylabel(‘v’) title(‘field [v,-x]’)Let’s plot the solution along with the field. This is what a solution looks like in state space. Compare to the 2 functions in time from Figure 1.figure quiver(xs,vs,dx,dv) hold on ts = 0:.1:10; % again matlab peculiarity that forces me to do it this way for now. xvs=f1(ts); plot(xvs(:,1),xvs(:,2),’r-‘) plot(xvs(1,1),xvs(1,2),’k*’, ‘MarkerSize’,10) % initial condition hold off axis square xlabel(‘x’) ylabel(‘v’) title(‘field [v,-x]’)If there are not explicit solutions, we can plot the results of simulations:df1a=@(t, xv) [xv(2); -xv(1)]; % ok, that’s how odeXX wants the function [ts, xvsym] = ode23(df1a, [0,10], [0,1]); plot(xvsym(:,1),xvsym(:,2),’rx-‘) xlabel(‘x’) ylabel(‘v’) axis square notice the roughness of the solution and the few points supporting it.Below, do the same investigation for problems 1 and 2 in Section 2.2., and problem 1 from the project. Add nullclines with contour or fcontour. Pick initial conditions for simulation after examining the field plot.2.2.1 % a) % b) % c) % d) 2.2.2% a) % b) Project problem 1Use\left\{x'(t)=\frac{y(t)}{25}-\frac{2 x(t)}{25},y'(t)=\frac{2 x(t)}{25}-\frac{y(t)}{25},x(0)=0,y(0)=0.5\right\} ODExx as a functionODE as a functionSo, you know how to use odeXX directly. Takes some coding, and tinkering, and re-execution. Here is one way that you can use it as a function. That is, one implementation of ‘the invisible function’ that I keep talking about – no formula, but we can do a lot of things with it.And ODE depends on the field and the initial condition. I’ll define a function for a specific field, but leave the initial condition as a parameter as well. I will use the logistic equation for the example (normalized to [0,1], but you see how you can put any field in it, even a vector field, and get a result. Of course, for a vector field, the initial condition x0 should also be a vector.df = @(t, x) x.*(1-x); tspan=[0,10]; fsim = @(t, x0) deval(ode45(df, tspan, x0),t)This implementation is not too safe, since it will try to evaluate t outside of tspan, which is not a good idea. We can easily fix it with an if/then statement.Now lets plot this function a few timesfplot({@(t) fsim(t,0.1), @(t) fsim(t, 0.2), @(t) fsim(t,0.5)},tspan)You can also have the function depend on other parameters, e.g. to investigate bifurcations.You should use this approach carefully, since the simulation may take a while for each function call, but in principle it behaves just like another matlab function… Here it is better to call with a list of times, rather than individual times, because you run the whole simulation in any case.Fitting simulations to dataLet me open up the model a little, add variable growth rate and capacity. I will generate some samples from one of these models, then pretend I don’t know which one I used and try to adjust the parameters for the simulator until a get a reasonable fit. I will do adjusting by hand here, but automation is not too difficult.dfrk = @(t, x, r, k) r.*x.*(1-x./k); tspan = [0 15]; % just repeating it, but could be different fsimrk = @(t, x0, r, k) deval(ode45(@(t, x) dfrk(t, x, r, k), tspan, x0),t) % note how I call ode with the more complex field function ts = tspan(1):.1:tspan(end); xs = fsimrk(ts, 0.2, 0.5, 1.5)+ 0.1*randn(size(ts)); % the last piece adds a little noiseNow that we have ‘observations’ (synthetic data) in xs and ts, let’s look at them, along with some simulations that we use for models.Task:Modify the parameters to get the model to fit the ‘data’.r=1.; k=1.; x0=.2; % here, play with r, k and x0 to get a better visual fit to the data. plot(ts, xs, ‘b.’) hold on fplot(@(t) fsimrk(t, x0, r, k),tspan,’r’) hold off xlabel(‘t’) ylabel(‘x’)NullclinesFrom Fig 2.2.11fimplicit(@(x, y) -x.^2+y,[-4 4 -1 9], ‘r-‘) hold on fimplicit(@(x, y) x-2,[-4 4 -1 9], ‘b-‘) hold off xlabel(‘S’) ylabel(‘I’)