function Lab3_prob2 clear; clc; close all; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Problem 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x=sym('x'); % Plotting a function. function f=V1(x) f= heaviside(x)-heaviside(x-1) + heaviside(x-2)-heaviside(x-3) + heaviside(x-4)-heaviside(x-5); end figure; ezplot(V1(x), [0,10,-1,2]) legend('Square wave') function f=V2(x) f= x.*(heaviside(x)-heaviside(x-1)) + (x-1).*(heaviside(x-1)-heaviside(x-2)) + (x-2).*(heaviside(x-2)-heaviside(x-3)); end figure; ezplot(V2(x), [0,10,-1,2]) legend('Saw-tooth wave') function f=V3(x) f= x.*(heaviside(x)-heaviside(x-1)) + (2-x).*(heaviside(x-1)-heaviside(x-3)) + (x-4).*(heaviside(x-3)-heaviside(x-5)) + + (6-x).*(heaviside(x-5)-heaviside(x-7)); end figure; ezplot(V3(x), [0,10,-1,2]) legend('Triangular wave') function dy=myODE(x,y) % In order to solve a 2nd order ODE in Matlab, we need to turn it into % a system of 2 1st order ODEs. dy1=y(2); %y_1' = y_2 dy2=-y(2)-3*y(1)+V1(x); % y_2' = the right hand side in y_1'' dy=[dy1;dy2]; end % initial conditions y(0)=y0_1 and y'(0)=y0_2 y0=[0; 0]; x=0:0.1:10; % Solving the ODE [x,y1]=ode45(@myODE, x, y0); figure; plot(x,y1(:,1),'-o') legend('electric current under square wave applied voltage') end