Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Simulation Portfolio For APM 3714

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Simulation portfolio for APM 3714

The portfolio must be submitted

as assignment number 3. That is tutorial 1, Tutorial 2,


Tutorial 3 and Exercise one and Exercise Two

Tutorials for APM 3714

Program TUTORIALS FOR AMP3714


These tutorial must be tried after you had an initial encounter with the
theoretical part of this module.
% This script determines the initial zero crossing of the
% underdamped response of a parallel RLC circuit. The
% underdamped response is:
% v(t)=exp(-t/(2RC))*(A*cos(sqrt(1/LC-(1/2RC)^2)t)
% + B*sin(sqrt(1/LC-(1/2RC)^2)t))
% The circuit component values and initial conditions
% are:
% R=100 ohms; L=1.0e-3 henry; C=1.0e-6 farads;
% A= 6.0 volt, B=-9.0 volt
% Solve over the interval 0 <= t <= 0.5e-3 sec
clear; clc;
R=100; L=1.0e-3; C=1.0e-6; A=6.0; B=-9;
fprintf('Example 4.1: Find first zero crossing of ');
fprintf('underdamped RLC circuit\n');
tmin=0.0; tmax=0.5e-3;
% split up timespan into 100 intervals:
N=100;
dt=(tmax-tmin)/N;
% First, calculate t and v(t) at each timestep
for n=1:N+1
t(n) = tmin+(n-1)*dt;
v(n) = func_RLC(t(n),A,B,R,L,C);
fprintf(' %10.4e %10.3f \n’,t(n),v(n);
end
plot(t,v), xlabel('t'), ylabel('v'),
title('v vs t for a RLC circuit'), grid;
% Next, use the search method to find the first timestep
% where the sign of v(t) changes. When found, use fzero
% to solve.
for n=1:N

sign = v(n)*v(n+1);

if sign <= 0.0


root_interval(1)=t(n);
root_interval(2)=t(n+1);
% Solve for the zero of the multi-argument
% function func_RLC.m by invoking func_RLC as an
% anonymous function of one variable.
root = fzero(@(t) func_RLC(t,A,B,R,L,C), ...
root_interval);
break;
end
end
fprintf('\n\n');
if n < N
fprintf('First zero crossing is at t=%10.4e s\n',...
root);
else
fprintf('No roots lie within %g <= t <= %g s\n',...
tmin,tmax);
end
% Check solution. Putting root into func_RLC should
% produce zero.
v = func_RLC(root,A,B,R,L,C);
fprintf('v=%8.6e \n',v);
----------------------------------------------------------------------------
% func_RLC.m
% This function works with Example_4_1.m
function v=func_RLC(t,A,B,R,L,C)
arg1=1/(2*R*C);
arg3=sqrt(1/(L*C)-1/(2*R*C)^2);
% Equation (4.5):
v=exp(-arg1*t)*(A*cos(arg3*t)+B*sin(arg3*t));

Look at the above programme and find out what is wrong. Discuss as
students and correct if necessary.
Program number 2. Use octave online to play with this programme.

% This program determines the roots of a polynomial using

% the built in function 'roots'.

% The first polynomial is: f=x^3-4.7*x^2-35.1*x+85.176.

% The roots of this polynomial are all real.

% The second polynomial is: f=x^3-9*x^2+23*x-65. The

% roots of this polynomial are both real and complex.

% Complex roots must be complex conjugates.

% To obtain more info on complex numbers in MATLAB, run

% "help complex" in the Command Window.

clear; clc;

% Define coefficients of first polynomial (real roots)

C(1)=1.0; C(2)=-4.7; C(3)=-35.1; C(4)=85.176;

fprintf('The first polynomial coefficients are:\n');

fprintf('The roots are: \n');

V=roots(C)

fprintf('The recalculated coefficients of the ');

fprintf('polynomial whose roots are V are:\n');

C_recalc=poly(V)

fprintf('\n\n');

% Define coefficients of second polynomial (real and

% complex roots)

D(1)=1.0; D(2)=-9.0; D(3)=23.0; D(4)=-65.0;

fprintf('The second polynomial coefficients are:\n');

fprintf('The roots are: \n');

W=roots(D)

fprintf('The real and imaginary parts of the ');

fprintf('roots are:\n');

re=real(W)

im=imag(W)

fprintf('The recalculated coefficients of the ');

fprintf('polynomial whose roots are W are:\n');

W_recalc=poly(W)
Look at the above programme and find out what is wrong. Discuss

Dear Students, please do work out the following questions in MATLAB or


OCTAVE
Tutorial one
Look at the following program and practice.
% This program illustrates the use of nested loops, i.e.
% an inner 'for' loop inside an outer 'for' loop.
% The program calculates e^x by both MATLAB's 'exp'
% command (variable 'ex2'), and by a Taylor series
% expansion (variable 'ex1'), where -0.5 < x < 0.5.
% The outer 'for' loop is used to determine the x
% values. The inner loop is used to determine the Taylor
% series method for evaluating e^x. In this example,
% term(n+1) is obtained by multiplying term(n) by x/n.
% The variable 'term' is established as a vector so that
% MATLAB's built-in 'sum' function can be used to sum
% all the terms calculated in the Taylor series method.
% A maximum of fifty terms is used in the series.
% Program output is sent both to the screen and to a
% file. By printing the output to a file, you can easily
% edit the output file to line up column headings,
% etc. (which you can't do when printing to the screen).
% Note: e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...
clear; clc;
xmin=-0.5; dx=0.1;
fo=fopen('output.txt','w');
% Table headings
fprintf(' x ex1 ex2 \n');
fprintf('-------------------------------------\n');
fprintf(fo,' x ex1 ex2 \n');
fprintf(fo,'-------------------------------------\n');
for i=1:11
x=xmin+(i-1)*dx;
ex2=exp(x);
term(1)=1.0;
for n=1:49
term(n+1)=term(n)*x/n;
if abs(term(n+1)) <= 1.0e-7
break;
end
end
ex1 = sum(term);
fprintf('%5.2f %10.5f %10.5f \n',x,ex1,ex2);
fprintf(fo,'%5.2f %10.5f %10.5f \n',x,ex1,ex2);
end

Tutorial number 2 Practice plotting multiple plots with octave or MATLAB


% Example_2_7.m
% This program creates a simple table and a simple plot.
% First, the vectors y1, y2 and t are calculated.
% Next a table of y1 = n^2/10 and y2 = n^3/100 is created.
% Then y1 and y2 are plotted.
clear; clc;
for n=1:10
t(n)=n;
y1(n)=n^2/10;
y2(n)=n^3/100;
end
% By making t, y1 and y2 as vectors, their values can be
% printed out outside the 'for' loop that created them.
% Print column headings
fprintf(' t y1 y2 \n');
fprintf('--------------------------------------\n');
for n=1:10
fprintf('%8.1f %10.2f %10.2f \n', ...
t(n),y1(n),y2(n));
end
% Create the plot. y1 is red, y2 is in green.
plot(t,y1,'r',t,y2,'g');
xlabel('t'), ylabel('y1,y2'), grid;
title('y1 and y2 vs. t');
% Plot identification is also established by adding text
% to the plot.
text(6.5,2.5,'y1');
% In the above statement, 6.5 is the abscissa position
% and 2.5 is the ordinate position where the 'y1' label
% will be positioned.
text(4.2,2.4,'y2');
% Other MATLAB plot options:
% Available color types:
% blue 'b'
% green 'g'
% red 'r'
% cyan 'c'
% yellow 'y'
% Curves y1 and y2 can also be distinguished.

Tutorials. Number 3

Use of subplot and elseif

% This program is an example of the use of the subplot


% command and the elseif ladder. Values of y1, y2, y3
% and y4 are constructed as vectors. Separate plots of
% y1 vs. t, y2 vs. t and y3 vs. t are plotted on the
% same page.
clc; clear;
for n=1:11
t(n)=n-1;
y1(n)=t(n)^2/10;
y2(n)=sin(pi*t(n)/10);
y3(n)=exp(t(n)/2);
y4(n)=sqrt(t(n));
end
for n=1:4
subplot(2,2,n)
if n==1
plot(t,y1), grid, title('y1 vs. t');
xlabel('t'), ylabel('y1');
elseif n==2
plot(t,y2), grid, title('y2 vs. t');
xlabel('t'), ylabel('y2');
elseif n==3
plot(t,y3), grid, title('y3 vs. t');
xlabel('t'), ylabel('y3');
elseif n==4
plot(t,y4), grid, title('y4 vs. t');
xlabel('t'), ylabel('y4');
end
end
----------------------------------------------------------------------------
Exercises which are supposed to submitted and will contribute towards your
assessment when calculated. So work hard towards them.
Exercise one
Write program that will compute several matrix operations, including addition,
dot product, element-by-element multiplication and division, sum, and transpose.
The matrix are a= [1, 5, 9,]
b = [2,6,12]

Exercise two:
2.1. Solve the following system of equation using the inverse matrix function and
the Gauss elimination method.
3X1+2X2-X3=10
-X1+3X2+2X3=5
X1-X2-X3=-1
Hint use: INV is the solution using matrix inverse function: INV=inv(A)*C
GAUSS is the solution using Gauss elimination method: Gauss=A\C

2.2. Plot the following equation in MATLAB or OCTAVE


To plot the quadratic x2+7x−3 from x equals −3 to 3 in steps of 0.2 we use the code

2.3.
Find all the zeroes of f(x) = x - tanx in the interval (0, 20) by the method of bisection.
Utilize the functions rootsearch and bisect in MATLAB or octave
RESOURCES
Dear Student,
please go to and practice on how to programme with MATLAB or OCTAVE.
Follow the links below:
https://app.knovel.com/web/view/khtml/show.v/rcid:kpNMESCCM3/cid:kt011QBMQA/v
iewerType:khtml/?b-q=matlab&view=collapsed&zoom=1&page=14

https://app.knovel.com/web/view/khtml/show.v/rcid:kpEMATLA08/cid:kt0112Y9T4/vie
werType:khtml//root_slug:2-matlab-fundamentals/url_slug:matlab-fundamentals?b-
q=matlab&sort_on=default&b-subscription=true&b-group-by=true&b-sort-on=default&b-
content-type=all_references&include_synonyms=no&b-toc-cid=kpEMATLA08&b-toc-root-
slug=&b-toc-url-slug=matlab-fundamentals&b-toc-
title=Essential%20MATLAB%20for%20Engineers%20and%20Scientists%20(6th%20Edition)
&page=4&view=collapsed&zoom=1

https://app.knovel.com/web/toc.v/cid:kpMATLABE1/viewerType:toc//root_slug:matlab-
electrical-computer/url_slug:matlab-electrical-computer?b-
q=matlab&sort_on=default&b-subscription=true&b-group-by=true&b-sort-on=default&b-
content-type=all_references&include_synonyms=no
https://www.youtube.com/watch?v=sHGqwF2s-tM
https://www.youtube.com/watch?v=bmE6SWE6c_A

you can find octave on one of the following links


https://octave-online.net/
https://www.gnu.org/software/octave/

You might also like