Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you change below matlab code by using different functions ? clear all;clc;close all; h=0.0625;tspan=[0 40];y0=[2 1];% Determines initial conditions and stepsize. a=1.2;b=0.6;c=0.8;d=0.3; %Determines constants

Can you change below matlab code by using different functions ?

clear all;clc;close all; h=0.0625;tspan=[0 40];y0=[2 1];% Determines initial conditions and stepsize. a=1.2;b=0.6;c=0.8;d=0.3; %Determines constants [t y] = eulersys(@predprey,tspan,y0,h,a,b,c,d);%Uses euler's system for ODE. subplot(2,2,1);plot(t,y(:,1),t,y(:,2),'--')% Makes a subplot and plots Prey-Predator graph. This is a time based figure for euler's method. legend('prey','predator');title('(a) Euler time plot') % Defines plot's information for a better visual inspection. subplot(2,2,2);plot(y(:,1),y(:,2)) % Makes a subplot and plots phase-plane figure for euler's method. title('(b) Euler phase plane plot')% Defines plot's information for a better visual inspection. [t y] = rk4sys(@predprey,tspan,y0,h,a,b,c,d); % Uses 4th order Runge-Kutta method. subplot(2,2,3);plot(t,y(:,1),t,y(:,2),'--') % Makes a subplot and plots a time based figure using 4th order Runge-Kutta method. title('(c) RK4 time plot')% Defines plot's information for a better visual inspection. subplot(2,2,4);plot(y(:,1),y(:,2))% Makes a subplot and plots phase-plane figure for 4th order Runge-Kutta method. title('(d) RK4 phase plane plot')% Defines plot's information for a better visual inspection. figure % Allows us to make multiple figures. tspan=[0 20];y0=[5 5 5];y1=[5.001 5 5]; % Defines new initial conditions. sigma=10;b=8/3;r=28; % Defines constants. [t y] = rk4sys(@lorenz,tspan,y0,0.03125,sigma,b,r);% Uses 4th order Runge-Kutta method for mentioned initial condition and constants. [t1 y1] = rk4sys(@lorenz,tspan,y0,0.03125,sigma,b,r);% Uses 4th order Runge-Kutta method for mentioned initial condition and constants. subplot(2,3,[1 2 3]) %Makes a subplot in the first row which takes 3 column worth space. plot(t,y(:,1),t1,y1(:,1),'--')% Plots the Lorenz model for x = y = z = 5 and x = 5.0001, y = z = 5 in the same figure. and takes the first row's three column worth of space. legend('x = y = z = 5','x = 5.0001, y = z = 5')% Defines plot's information for a better visual inspection. title('(a) Lorenz model x versus t');% Defines plot's information for a better visual inspection. subplot(2,3,4);plot(y(:,1),y(:,2))%Plots a graph with horizontal axis as x and vertical axis as y coordinates. xlabel('x');ylabel('y')% Defines plot's information for a better visual inspection. axis square;title('(b) y versus x')% Defines plot's information for a better visual inspection. subplot(2,3,5);plot(y(:,1),y(:,3))%Plots a graph with horizontal axis as x and vertical axis as z coordinates. xlabel('x');ylabel('z')% Defines plot's information for a better visual inspection. axis square;title('(c) z versus x')% Defines plot's information for a better visual inspection. subplot(2,3,6);plot(y(:,2),y(:,3))%Plots a graph with horizontal axis as z and vertical axis as y coordinates. xlabel('y');ylabel('z')% Defines plot's information for a better visual inspection. axis square;title('(d) z versus y')% Defines plot's information for a better visual inspection. figure % Allows us to make a new figure. plot3(y(:,1),y(:,2),y(:,3)) % This plots a three-dimensional phase-plane graph. xlabel('x');ylabel('y');zlabel('z');grid; % Enables grid lines and defines plot's information for a better visual inspection. function yp = predprey(t,y,a,b,c,d) % This function makes our ordinary differential equation for Prey-Predator graph. yp = [a*y(1)-b*y(1)*y(2);-c*y(2)+d*y(1)*y(2)]; end function [tp,yp] = rk4sys(dydt,tspan,y0,h,varargin) % Function for 4th order Runge-Kutta method. % rk4sys: fourth-order Runge-Kutta for a system of ODEs % [t,y] = rk4sys(dydt,tspan,y0,h,p1,p2,...): integrates % a system of ODEs with fourth-order RK method % input: % dydt = name of the M-file that evaluates the ODEs % tspan = [ti, tf]; initial and final times with output % generated at interval of h, or % = [t0 t1 ... tf]; specific times where solution output % y0 = initial values of dependent variables % h = step size % p1,p2,... = additional parameters used by dydt % output: % tp = vector of independent variable % yp = vector of solution for dependent variables if nargin<4,error('at least 4 input arguments required'), end % If number of arguments entered is less than 4, makes an error. if any(diff(tspan)<=0),error('tspan not ascending order'), end % If the derivative of the tspan matrix is descanding, makes an error. n = length(tspan); % Determines the n as the length of the time condition. ti = tspan(1);tf = tspan(n); % Defines initial and final values for time condition. if n == 2 % Sets an if statement for n=2. t = (ti:h:tf)'; n = length(t); % If n equals to 2, sets t as a if t(n)h,hh = h;end % If hh value is actually bigger than step size, this reduces it to the original step size. while(1) % Opens a while loop which starts repating itself with no condition whatsoever. if tt+hh>tend,hh = tend-tt;end % Opens an if statement for tt+hh is bigger than the next time value. If this is the case, it sets hh as the difference between new time value and initial time condition. k1 = dydt(tt,y(i,:),varargin{:})'; % Defines the k1 value of the 4th order Runge-Kutta method. ymid = y(i,:) + k1.*hh./2; % Defines the y value for 4th order Runge-Kutta method. k2 = dydt(tt+hh/2,ymid,varargin{:})';% Defines the k2 value of the 4th order Runge-Kutta method. ymid = y(i,:) + k2*hh/2;% Defines the y value for 4th order Runge-Kutta method. k3 = dydt(tt+hh/2,ymid,varargin{:})';% Defines the k3 value of the 4th order Runge-Kutta method. yend = y(i,:) + k3*hh;% Defines the y value for 4th order Runge-Kutta method. k4 = dydt(tt+hh,yend,varargin{:})';% Defines the k4 value of the 4th order Runge-Kutta method. phi = (k1+2*(k2+k3)+k4)/6; % Defines the phi value which is an increment function. y(i+1,:) = y(i,:) + phi*hh;% Defines the next y value for 4th order Runge-Kutta method. tt = tt+hh; % Changes tt for the next iteration and sets it to the next value by adding the step size to it. i=i+1; % Changes the iteration number to the next value by adding 1. if tt>=tend,break,end % If the current time is greater than the final time condition breaks the iteration loop. end np = np+1; tp(np) = tt; yp(np,:) = y(i,:); % Sets the constants for the next iteration and changes the axis we are working on as well. if tt>=tf,break,end % If the current time is greater than the final time condition breaks the iteration loop. end end function [t,y] = eulersys(dydt,tspan,y0,h,varargin) % Uses Eulers method for ordinary differential systems. % [t,y] = eulersys(dydt,tspan,y0,h): % uses Eulers method to integrate a system of ODEs % input: % dydt = name of the M-file that evaluates the ODEs % tspan = [ti, tf] where ti and tf = initial and % final values of independent variable % y0 = initial values of dependent variables % h = step size % output: % t = vector of independent variable % y = vector of solution for dependent variables ti = tspan(1); tf = tspan(2); % sets initial and final values for the initial time condition. t = (ti:h:tf)'; % Defines a new time matrix with specified step size. n = length(t); % Defines n as the length of the time. if t(n)h,hh = h;end % If hh value is actually bigger than step size, this reduces it to the original step size. while(1) if tt+hh>tend,hh = tend-tt;end % Opens an if statement for tt+hh is bigger than the next time value. If this is the case, it sets hh as the difference between new time value and initial time condition. k1 = dydt(tt,y(i,:),varargin{:})'; % Defines the k1 value of the midpoint method. ymid = y(i,:) + k1*hh/2; % Defines the y value for calculations. k2 = dydt(tt+hh/2,ymid,varargin{:})'; % Defines the k2 value of the midpoint method. y(i+1,:) = y(i,:) + k2*hh; tt = tt+hh; % Changes tt for the next iteration and sets it to the next value by adding the step size to it. i=i+1; % Changes the iteration number to the next value by adding 1. if tt>=tend,break,end% If the current time is greater than the final time condition breaks the iteration loop. end np = np+1; tp(np) = tt; yp(np,:) = y(i,:);% Sets the constants for the next iteration and changes the axis we are working on as well. if tt>=tf,break,end % If the current time is greater than the final time condition breaks the iteration loop. end end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions

Question

Additional Factors Affecting Group Communication?

Answered: 1 week ago