Question
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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started