0% found this document useful (0 votes)
30 views86 pages

769700-Chapter 5&6

The document contains various MATLAB functions and scripts for performing calculations related to electrical engineering, including resistor combinations, Monte Carlo simulations, plotting functions, and checking for prime numbers. It also includes examples of loops, conditionals, and mathematical operations. The code is structured into multiple functions, each serving a specific purpose in the context of electrical circuit analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views86 pages

769700-Chapter 5&6

The document contains various MATLAB functions and scripts for performing calculations related to electrical engineering, including resistor combinations, Monte Carlo simulations, plotting functions, and checking for prime numbers. It also includes examples of loops, conditionals, and mathematical operations. The code is structured into multiple functions, each serving a specific purpose in the context of electrical circuit analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

disp('Hello world')

v = [1 2 3 4]; disp(v)

for i = 1 : 10
disp('ECE is fun');
end
function problem2(r)
for r2 = [10 15 22 33 47 68 82]
rp = (*** a function of r and r2 ***);
disp(rp)
end

function ForIndexingVectorExample(r)
vAllResistors = [10 15 22 33 47 68 82];
for i = 1:length(vAllResistors)
rCur = vAllResistors(i);
rp = (r*rCur)/(r+rCur);
disp(rp)
end
function plotStepFunction() % takes and returns
% no arguments
vt = linspace(-2, 3, 1000); % horizontal axis
% has 1000 points
vu = zeros(1,1000); % create vertical data vector
% of same size
for i = 1:length(vu)
t = vt(i); % t loops through each point
% in vector vt
if (t<0) % if t is negative
vu(i) = 0; % then vu at that time is 0
else % otherwise
vu(i) = 1; % vu at that time is 1
end
end
plot(vt,vu)

function plotPiecewiseFunction()
vt = linspace(0,30,1000); % let t range from 0 to 30
vf = zeros(1,1000); % fill with 0’s
for i = 1 : 1000
t = vt(i); % iterate so t will be
% every value in vt
if (t >= 0) && (t < 10)
vf(i) = sqrt(10*t);
elseif (t >= 10) && (t < 20)
vf(i) = 10*exp(10-t);
elseif (t >= 20)
vf(i) = t-20;
end
end
plot(vt,vf)
1 t, t 0
vt
et 4 , t 0
f 1
2 RC

function result = MonteCarlo(N)


% N is the number of simulations desired
result = zeros(1,N); % fill result with
% N zeros
for i = 1 : N % do the simulation N times
R = 1000 + 1000*(rand*0.1-0.05);
% ±5% resistor tolerance
C = 1e-6 + 1e-6*(rand*0.2-0.1);
% ±10% capacitor tolerance
f = 1/(2*pi*R*C);
result(i) = f;
end

result = MonteCarlo(1000);
plot(result,'.')
xlabel('simulation number')
ylabel('critical frequency')
histogram(result); % plot the histogram
xlabel('critical frequency')
ylabel('number')
function FindResistors()
% FindResistors finds all combos of 2 resistors
% given 4 types
vr = [10 22 47 51]; % 4 types of stocked resistors
for i1 = 1:4 % the first resistor index
R1 = vr(i1); % R1 is the first resistor value
for i2 = 1:4 % choose the second resistor index
R2 = vr(i2); % R2 is the second resistor value
fprintf('R1 = %g, R2 = %g\n’, R1, R2)
end
end
c a2 b2
function [R1best, R2best] = findclosestseries(rDesired)
% create vR with standard value resistors from 1 to 9.1
vR = [1 1.1 1.2 1.3 1.5 1.6 1.8 2 2.2 2.4 2.7 3 ...
3.3 3.6 3.9 4.3 4.7 5.1 5.6 6.2 6.8 7.5 8.2 9.1];
% ... wraps the line

% duplicate vR by multiples of 10 to create all


% standard value Rs
vR = [0 vR vR*10 vR*100 vR*1e3 vR*1e4 vR*1e5 1e6];
R1best = 0; % we haven’t yet found the best value of R1
R2best = 0; % we haven’t yet found the best value of R2
bestError = 1e12; % our initial error is huge

% loop through to find every possible value of R1


for i1 = 1:length(vR)
R1 = vR(i1);
% loop through to find every possible value of R2
for i2 = 1:length(vR)
R2 = vR(i2);
Rseries = R1+R2;
currentError = abs(Rseries – rDesired);
if (currentError <bestError)
% we found a better solution
R1best = R1;
R2best = R2;
bestError = currentError;
end
end
end
function calculationspeed
% find how long it takes to create two random 50x50
% matrices and multiply them together
tic
a = rand(50,50); % create two random 50x50 matrices
b = rand(50,50);
c = a*b; % multiply them together
toc
function z = polar2complex(mag, angle)
% polar2complex returns a complex number given
% the magnitude and angle in degrees
z = mag*cosd(angle) + j*mag*sind(angle);

function p = parallel(r1, r2)


% parallel returns the parallel resistance
% of two given resistors
p = (r1*r2)/(r1+r2);
function b = isInteger(x)
b = x == floor(x); % b is 1 if x is an integer,
% otherwise 0

function b = isPrime(n)
b = 1; % assume it is prime, then check
for f=2:floor(sqrt(n)) % f cycles through
if isInteger(n/f) % is f a factor of n?
b = 0; % n is not prime
break % break out of the loop
end
end
function b = isPrime2(n)
b = 1; % assume it is prime, then check
for f=2:floor(sqrt(n)) % f cycles through
% all possible factors
if isInteger(n/f) % is f a factor of n?
b = 0; % change b to 0 to show n is not prime
break; % break out of the loop
end
end
%
% helper functions
function b = isInteger(x)
b = (x == floor(x)); % b is 1 if x is an integer,
% otherwise 0

while(logical test statement)


program code to be run
end

function sum=WhileExample(x)
% WhileExample will loop until new additions are < x
term = 1;
sum = 0;
while (term >= x) % run until the terms are less than x
term = term/2;
sum = sum+term;
end
1+1+1
2 4 8

1 1 1
3 9 27
bin2dec()

bin2dec('1010')

dec2bin(181)
1 t, t 0
v t
2e t-1, t 0

1 1
vin 2 vin 1
2 2
vout vin , 1 vin 1
1 1
v 1 v in 2
2 in 2
[t,v] = problem03(21, 2, 10)
plot(t,v)

1 1 2x N
cos , 0 x
2 2 N 2
N N
y x 1, x N
2 2
1 1 2x 2 N
cos , N x N
2 2 N 2
fc 1
2 R1R2C1C2
V1 = Vin R1
R1 + R2
N n
1
t cos 7n t
n=0 2

sin t ,0 t
y(t )
0, 0.9 t
an_1
, an even
an 2
3an_1 +1, an odd
1
0
1

1 (V0 1)e kt , 0 t 1
(1 V0 )e k (t 1) , 1 t 2
v(t)
1 (V0 1)e k (t 2)
, 2 t 3
(1 V0 )e k (t 3) , 3 t 4
N
12
f N k
k= 0
3 2k 1
49
sin kt
f
k 1 3 5 k
This page intentionally left blank

You might also like