1. Engineering
  2. Computer Science
  3. how do you plot these four codes on one set...

Question: how do you plot these four codes on one set...

Question details

How do you plot these four codes on one set of axes? You might not need the codes to tell me how but I listed them anyway.

*****Codes:

Euler's Method:

function Euler()

% We will use Euler's method to investigate the first order DE dy/dt=cos(t-y) with initial condition y(0)=1

clear t % Clears all t inputs
clear y % Clears all y inputs

ti=0; % This is the left endpoint of our interval, t initial

h=1; % This is the step size, distance between t values

tf=100; % This is the right endpoint of our interval, t final

n=(tf-ti)/h; % This is the total amount of steps

t(1)=ti; % Declares the starting t value for the loop

y(1)=1; % This is the value of the initial condition at the first iteration of the loop

for j=1:n
    t(j+1)=h+t(j);
    y(j+1)=y(j)+h*cos(t(j)-y(j));
end

figure(1)

plot(t,y)

xlabel('t')
ylabel('y(t)')

title('Euler')

hold on % Allows plotting multiple solutions on the same set of axes
grid on % Outputs xy-axes for plot

Improved Euler:

function ImprovedEuler()

% We will use improved Euler's method, the one that corresponds to the midpoint method,
% to investigate the first order DE, dy/dt=cos(t-y) with initial condition y(0)=1

clear t % Clears all t inputs
clear y % Clears all y inputs

ti=0; % This is the left endpoint of our interval, t initial

h=1; % This is the step size, distance between t values

tf=100; % This is the right endpoint of our interval, t final

n=(tf-ti)/h; % This is the total amount of steps

t(1)=ti; % Declares the starting t value for the loop

y(1)=1; % This is the value of the initial condition at the first iteration of the loop

for j=1:n
    t(j+1)=h+t(j);
    f(j)=cos(t(j)-y(j));
    y(j+1)=y(j) + h*f(j)/2 + h*cos(t(j)+h-y(j)-(h*f(j)))/2;
end

figure(2)

plot(t,y)

xlabel('t')
ylabel('y(t)')

title('Improved Euler')

hold on
grid on

Runge Kutta:

function RungeKutta()

% We will use Runge-Kutta, the method that corresponds to the trapezoidal weighted average,
% to investigate the first order DE dy/dt=cos(t-y) with initial condition y(0)=1

clear t % Clears all t inputs
clear y % Clears all y inputs

ti=0; % This is the left endpoint of our interval, t initial

h=1; % This is the step size, distance between t values

tf=100; % This is the right endpoint of our interval, t final

n=(tf-ti)/h; % This is the total amount of steps

t(1)=ti; % Declares the starting t value for the loop

y(1)=1; % This is the value of the initial condition at the first iteration of the loop

for j=1:n
    t(j+1)=h+t(j);
    f(j)=cos(t(j)-y(j));
    
    k1(j)=f(j);
    k2(j)=cos(t(j)+h/2-y(j)-(h*k1(j)/2));
    k3(j)=cos(t(j)+h/2-y(j)-(h*k2(j)/2));
    k4(j)=cos(t(j)+h-y(j)-(h*k3(j)));
    
    y(j+1)=y(j)+(h*k1(j)/6)+(h*k2(j)/3)+(h*k3(j)/3)+(h*k4(j)/6);
end

figure(3)

plot(t,y)

xlabel('t')
ylabel('y(t)')

title('Runge-Kutta')

hold on
grid on

Plot Code:

% For loop to plot temperature vs time
for A=115:4:125
T=0:100
B=(98.6-A)*exp(-0.054*T)+A;
plot(T,B);
hold on
end

title('Body Temperature vs Time');
xlabel('Time (min)');
ylabel('Body Temperature (Fahenheit)');
grid on;

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution