Simulink Models

Simulink Models

Solve each of the following 4 initial value problems (IVP) via Simulink blocks.

  1. y` = dy/dx = 3 x2 for 2 ≤ x ≤ 4 with y = 0.5 at x = 2
  2. y` = dy/dx = -0.131 y for 0 ≤ x ≤ 5 with y = 4 at x = 0
  3. y` = 2 x cos2(y) for 0 ≤ x ≤ 2 with y = π/4 at x = 0
  4. y“ = d2y/dx2 = y` (1 – y2) – y for 0 ≤ x ≤ 20 with y`(0) = 0, y(0) = 0.25
  • Create 4 Simulink models to solve the 4 initial value problems. Each Simulink model should contain a Simulink blocks solution.
  • Save the 4 Simulink blocks solutions’ data into 4 different .mat files.
    • Save x, dy/dx, and y for equation 1, 2, and 3.
    • Save x, d2y/dx2, dy/dx, and y for equation 4.
  • Write a MATLAB driver program that will open all 4 Simulink models (for the 4 initial value problems above) and automatically run each Simulink model. Load the Simulink results from the simulation models to the MATLAB driver program.
    • Load x, dy/dx, and y for equation 1, 2, and 3.
    • Load x, d2y/dx2, dy/dx, and y for equation 4.
  • Use MATLAB to plot a total of 4 figure windows, 1 figure for each equation, and open all four figures on screen simultaneously.
    • Plot the solutions created by Simulink.
    • Make use of the plot titles, legends, and label axes properly to distinguish the results.
    • Plot x vs. dy/dx and x vs. y for the Simulink solutions for Equations 1, 2, and 3.
    • Plot x vs. d2y/dx2, x vs. dy/dx, and x vs. y for the Simulink solution for Equation 4.

Solution 

%% Homework 6 Initial Value Problem via Simulink and Matlab

%% Clear everything and close all figures

clc,clear,close all;

%% Run all 4 Simulink models

simOut1 = sim(‘ODE_1’);

simOut2 = sim(‘ODE_2’);

simOut3 = sim(‘ODE_3’);

simOut4 = sim(‘ODE_4’);

clearsimOut* % Clear the simOut variables

%% Load the .mat files which have the variables from Simulink

load ODE1_dydx.mat

load ODE1_x.mat

load ODE1_y.mat

load ODE2_dydx.mat

load ODE2_x.mat

load ODE2_y.mat

load ODE3_dydx.mat

load ODE3_x.mat

load ODE3_y.mat

load ODE4_dy_dx_sq.mat

load ODE4_dydx.mat

load ODE4_x.mat

load ODE4_y.mat

delete *.mat % delete the .mat files since they are no longer required

figure(1)

plot(y_ode1.Time,y_ode1.Data,’b-‘); hold on;

plot(dydx_ode1.Time,dydx_ode1.Data,’r–‘); grid on;

xlabel(‘x’); ylabel(‘f(x)’);

legend(‘y(x)’,’dy/dx’);

title(‘Plots for ODE: y” = 3x^2 with y(2) = 0.5’);

figure(2)

plot(y_ode2.Time,y_ode2.Data,’b-‘); hold on;

plot(dydx_ode2.Time,dydx_ode2.Data,’r–‘); grid on;

xlabel(‘x’); ylabel(‘f(x)’);

legend(‘y(x)’,’dy/dx’);

title(‘Plots for ODE: y” = -0.131y with y(0) = 4’);

figure(3)

plot(y_ode3.Time,y_ode3.Data,’b-‘); hold on;

plot(dydx_ode3.Time,dydx_ode3.Data,’r–‘); grid on;

xlabel(‘x’); ylabel(‘f(x)’);

legend(‘y(x)’,’dy/dx’);

title(‘Plots for ODE: y” = 2xcos^2(y) with y(0) = \pi/4’);

figure(4)

plot(y_ode4.Time,y_ode4.Data,’b-‘); hold on;

plot(dydx_ode4.Time,dydx_ode4.Data,’r-‘); hold on;

plot(dy_dx_sq_ode4.Time,dy_dx_sq_ode4.Data,’g-‘); grid on;

xlabel(‘x’); ylabel(‘f(x)’);

legend(‘y(x)’,’dy/dx’,’d^2y/dx^2′);

title(‘Plots for ODE: y”” = y”(1-y^2) – y  with y”(0) = 0 and y(0) = 0.25’);