0% found this document useful (0 votes)
9 views8 pages

first-exam-1

first-exam-1

Uploaded by

Mohamad Ahmed
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)
9 views8 pages

first-exam-1

first-exam-1

Uploaded by

Mohamad Ahmed
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/ 8

%15/1/2023 fina

%% q1 a
clc
clear
close all

disp('1- default values');


disp('2- custome values');
choose = input('enter your choice: ') ;

if choose == 1
r1 = 2 ;
r2 = 3 ;
r3 = 4 ;
v1 = 7 ;
v2 = 6 ;

elseif choose == 2
r1 = input('enter r1: ') ;
r2 = input('enter r2: ') ;
r3 = input('enter r3: ') ;
v1 = input('enter v1: ') ;
v2 = input('enter v2: ') ;

else
disp('inavlid choice')
end

a = [ r1+r2 r1 -r2; -r1 r1+r2+r3 -r3; -r2 -r3 r1+r2+r3] ;


b =[] ;
c = [ -v1+v2 0 v2] ;

b = inv(a) .* c ;
fprintf('the value of I1 is: %d\n',b(1,1) ) ;
fprintf('the value of I2 is: %d\n',b(1,2) ) ;
fprintf('the value of I3 is: %d\n',b(1,3) ) ;

%% q1 b
clc
clear
close all
vi = 10 ;
vo = -100 ;
av = vo / vi ;
rs = 1 ;
rf = rs*av ;
wh = 10000 ;
cf = 1/(wh*tf) ;
fprintf('the gain is: %d\n',av);
fprintf('the feedback resistance is: %d]n', fr);
fprintf('the feedback capacitor is : %d\n', rf) ;
%% q2 parachuit (analytical vs numerical)
clc
clear
close all
format short;
g = 9.8;
cd = 12.5 ;
m = 68.1;
dt = input('enter time increment') ;
tf = input('enter final time') ;
% analytical
t = 0 :dt :tf ;
v = g*m/cd*(1-exp((-cd/m)*t));

%numerical
ti = 0:dt:tf;
ns = length(t) ;
vi(1) = 0 ;
for n1 = 2 : ns
vi(n1) = vi(n1-1) + (g- (cd/m) *vi (n1-1))*(n1-n1+1) ;
end
plot(t,v,'r',ti,vi,'b')
%% q3
clc
clear
close all
% Main program
stopMark = 'stop'; % Marker to stop the program

while true
% Get user input for n or stop mark
user_input = input('Enter a value for n or type "stop" to end: ',
's');

% Check if the user wants to stop


if strcmpi(user_input, stopMark)
disp('Program terminated.');
break;
end

% Convert user input to a number


n = str2double(user_input);

% Check if the input is a valid number


if isnan(n) || ~isreal(n)
disp('Invalid input. Please enter a valid number or "stop" to
end.');
continue;
end

% Calculate g1(n)
f_n = fibonacci(n);
n_factorial = factorial(n);
g1_n = (f_n * sin(2 * pi * n)) / (n_factorial * cos(pi * n));

% Display the result


fprintf('g1 = %s\n',num2str(g1_n)) ;
%disp(['g1(', num2str(n), ') = ', num2str(g1_n)]);
end

% Function to calculate the Fibonacci sequence


function fib = fibonacci(n)
if n == 0
fib = 0;
elseif n == 1
fib = 1;
else
fib = fibonacci(n-1) + fibonacci(n-2);
end
end

% Function to calculate the factorial of a number


function fact = factorial(n)
fact = 1;
for i = 1:n
fact = fact * i;
end
end

%% q4 a
clc
clear
close all

syms t T

% First equation
ft1 = 3 * sin(5 * pi * t);

% Energy test
e1 = int(abs(ft1)^2, t, -inf, inf);

% Power test
% **Corrected the integration limits to include t:**

p1 = limit(int(abs(ft1)^2, t, -T/2, T/2) / T, T, inf);

% Second equation
ft2 = 5 * exp(-2 * abs(t));

% Energy test
e2 = int(abs(ft2)^2, t, -inf, inf);

% Power test
% **Corrected the integration limits to include t:**
p2 = limit(int(abs(ft2)^2, t, -T/2, T/2) / T, T, inf);

ee1 = isfinite(e1);
pp1 = isfinite(p1);
ee2 = isfinite(e2);
pp2 = isfinite(p2);

% **Added parentheses for clarity in conditional expressions:**


if (ee1 == 1 && pp1 == 1)
disp('f1(t) is neither energy nor power signal')
fprintf('the Energy is : %d\n',e1) ;
fprintf('the Power is : %d\n',p1) ;
elseif (ee1 == 1 && pp1 ~= 1)
disp('f1(t) is energy signal')
fprintf('the Energy is : %d\n',e1) ;
elseif (pp1 == 1 && ee1 ~= 1)
disp('f1(t) is power signal')
fprintf('the Power is : %d\n',p1) ;
end

if (ee2 == 1 && pp2 == 1)


disp('f2(t) is neither energy nor power signal')
fprintf('the Energy is : %d\n',e2) ;
fprintf('the Power is : %d\n',p2) ;
elseif (ee2 == 1 && pp2 ~= 1)
disp('f2(t) is energy signal')
printf('the Energy is : %d\n',e2) ;
elseif (pp2 == 1 && ee2 ~= 1)
disp('f2(t) is power signal')
fprintf('the Power is : %d\n',p2) ;
end

%% q4 b signals

clc
clear
close all
%impulse
t1 = -10 :0.001: 10 ;
start = input('enter start of impulse: ') ;
impulse = zeros(1,length(t1)) ;
for i = 1 :length(t1)
if t1(i) == start
impulse(i) = 1 ;
else
impulse(i) = 0 ;
end
end
%step
t2 = -10: 10 ;
%n = 5
start = input('enter the start of step: ') ;
step = zeros (1,length(t2)) ;
for i = 1:length(t2)
if t2(i) > start % n
step(i) = 1 ;
else
step(i) = 0 ;
end
end
%rectangle
t3 = -10 :0.001: 10 ;
start = input('enter start for rectangle : ') ;
the_end = input('enter end of rectangle: ');
rect = zeros(1,length(t3)) ;
for i = 1:length(t3)
if (t3(i)>=start && t3(i)<=the_end)
rect(i) = 1 ;
end
end
%triangle
t4 = -10 :0.001: 10 ;
tri = zeros(size(t4));
for i = 1 : length(t4)
if t4(i) < 0
tri(i)= t4(i)+1;
else
tri (i) = -t4(i) +1;
end
end
%sawtooth
t5 = -10:0.001:10 ;
f = 0.1;
saw = sawtooth(2*pi*f*t5) ;
%square wave
t6=0:0.01:0.99;
f = 5;
sqr = square(2*pi*f*t6) ;
%sinc function
t7 = -10:0.001:10 ;
f = 0.1;
sin_c = sinc(2*pi*f*t7) ;

%convolution of two triangles


% Define triangular signals
t8 = -5: 0.1: 5 ;
f_t = max(0, 1 - abs(t8));
g_t = max(0, 1 - abs(t8));

% Convolve the triangular signals


conv_result = conv(f_t, g_t, 'same');

figure ;

subplot(2,4,1)
plot(t1,impulse,'r')
title('impulse')

subplot(2,4,2)
plot(t2,step,'g')
title('step')

subplot(2,4,3)
plot(t3,rect,'b')
title('rectangle')

subplot(2,4,4)
plot(t4,tri,'y')
title('triangle')

subplot(2,4,5)
plot(t5,saw,'b')
title('sawtooth')

subplot(2,4,6)
plot(t6,sqr,'c')
title('square pulse')

subplot(2,4,7)
plot(t7,sin_c,'m')
title('sinc')

subplot(2, 4, 8);
plot(t8, conv_result, 'g-');
title('Convolution Result (f * g)');
% calculating spectrum

impulseSpectrum = fftshift(fft(impulse));
stepSpectrum = fftshift(fft(step));
rectangleSpectrum = fftshift(fft(rect));
triangleSpectrum = fftshift(fft(tri));
sawtoothSpectrum = fftshift(fft(saw));
squareWaveSpectrum = fftshift(fft(sqr));
sincSpectrum = fftshift(fft(sin_c));
convolutionSpectrum = fftshift(fft(conv_result));

% Plot spectrum
figure;

subplot(4, 2, 1);
plot(abs(impulseSpectrum));
title('Impulse Spectrum');

subplot(4, 2, 2);
plot(abs(stepSpectrum));
title('Step Spectrum');

subplot(4, 2, 3);
plot(abs(rectangleSpectrum));
title('Rectangle Spectrum');

subplot(4, 2, 4);
plot(abs(triangleSpectrum));
title('Triangle Spectrum');

subplot(4, 2, 5);
plot(abs(sawtoothSpectrum));
title('Sawtooth Spectrum');

subplot(4, 2, 6);
plot(abs(squareWaveSpectrum));
title('Square Wave Spectrum');

subplot(4, 2, 7);
plot(abs(sincSpectrum));
title('Sinc Spectrum');

subplot(4, 2, 8);
plot(abs(convolutionSpectrum));
title('Convolution Spectrum');

%% q5

%% q6 sum

clc
clear
close all
syms a b c
xf=6 ;%input('Enter the final: ');

xi=[0 1 2 3 4 5];
yi=[2.1 7.7 13.6 27.2 40.9 61.1];

a1=[];
a2=[];
a3=[];
a4=[];
a5=[];
a6=[];
a7=[];
sum=0;
sum1=0;
sum2=0;
sum3=0;
sum4=0;
sum5=0;
sum6=0;
sum7=0;

for i=1:xf
a22(i)=xi(i);
sum1=sum1+a22(i);
end

for i2=1:xf
a2(i2)=xi(i2)^2;
sum2=sum2+a2(i2);
end
for i3=1:xf
a3(i3)=xi(i3)^3;
sum3=sum3+a3(i3);
end

for i4=1:xf
a4(i4)=xi(i4)^4;
sum4=sum4+a4(i4);
end
for i5=1:xf
a5(i5)=yi(i5);
sum5=sum5+a5(i5);
end
for i6=1:xf
a6(i6)=yi(i6)*xi(i6);
sum6=sum6+a6(i6);
end
for i7=1:xf
a7(i7)=yi(i7)*(xi(i7)^2);
sum7=sum7+a7(i7);
end
f1=a*xf+b*sum1+c*sum2==sum5;
f2=a*sum1+b*sum2+c*sum3==sum6;
f3=a*sum2+b*sum3+c*sum4==sum7;
[a,b,c]=solve(f1,f2,f3);
syms x e
for w=1:length(yi)
eq3=e+yi(w);
end
eq2=e+a+b*x+c*x^2;
[ x, e ] = solve(eq2 , eq3) ;

You might also like