You are on page 1of 9

Problem 1:

clc;
clear all;
close all;

h = 0.01; % Step size


x = -sqrt(2):h:sqrt(2); % Define x values

f = @(x, y) x * (y - 1); % Define the function

r1 = 0;
for i = 1:length(x)
y = x(i)^2 - 3:h:1 - x(i)^2;
% Trapezoidal rule for integrating with respect to y
r1(i) = (h / 2) * (sum(f(x(i), y)) + sum(f(x(i), y(2:end-1))));
end

% Trapezoidal rule for integrating with respect to x


r2 = (h / 2) * (sum(r1) + sum(r1(2:end-1)));

% Now you might want to annotate or plot the results, such as:
fprintf('Result of the double integral: %.4f\n', r2);

The numerical computation of the integral ∫x(y-1) over a symmetric region is anticipated to yield an exact
result of 0 due to the cancellation of positive and negative contributions. This cancellation is a consequence
of the function's characteristics, where (y-1) changes sign when y transitions across 1, combined with the
symmetry of the linear term x around the origin. The integration region and its boundaries exhibit symmetry
with respect to the x-axis, further supporting the expectation of a precise integral value. However, despite
these inherent symmetries, minor deviations from the exact result (0) may occur in numerical computations.
These discrepancies can be attributed to errors introduced during discretization using methods like the
trapezoidal rule and the finite grid employed for integration, which may lead to slight inaccuracies in the
final numerical output.

Problem 2:

clc; close all;

% Define functions f(t) and g(t)


f = @(t) (1 - abs(t)) .* (abs(t) < 1);
g = @(t) (1 - abs(t)) .* (abs(t) < 1);

% Define time grid


t = -5:0.01:5;

% Initialize convolution result


convolution = zeros(size(t));

% Perform convolution using trapezoidal rule


for i = 1:length(t)
tau = -5:0.01:5;
integrand = f(tau) .* g(t(i) - tau);
convolution(i) = trapz(tau, integrand);
end

% Plot the convolution


figure;
plot(t, convolution, 'b', 'LineWidth', 2);
xlabel('t','FontSize',15);
ylabel('f * g(t)','FontSize',15);
title('Convolution of f(t) and g(t)','FontSize',15);
grid on;
Problem 3(a):

When analyzing a circuit with diodes using Kirchhoff's Voltage Law (KVL), it is crucial to consider
the bias conditions of the diodes. In the positive half cycle where D1 is forward biased and D2 is
reverse biased, the KVL equation can be expressed as -Vi + IDR + VD + 8 = 0. Substituting the
diode current expression ID = Is (ⅇ𝑣𝐷∕𝑣𝑇 -1) into this equation yields -Vi + Is (ⅇ𝑣𝐷∕𝑣𝑇 -1) × R + VD
+ 8 = 0. On the other hand, during the negative half cycle when D2 is forward biased and D1 is
reverse biased, the KVL equation becomes Vi + IDR + VD + 6 = 0, which can be further refined by
substituting ID = Is (ⅇ𝑣𝐷∕𝑣𝑇 -1) to get Vi + Is (ⅇ𝑣𝐷∕𝑣𝑇 -1) × R + VD + 6 = 0. These equations
capture the conditions when diodes are either forward or reverse biased, providing a way to
express diode current in terms of diode voltage accurately.

Problem 3(b):
clc;
clear all;
close all;

% Define given values


R = 5e3; % Resistance (ohms)
Is = 0.1e-12; % Saturation current (A)
VT = 25e-3; % Thermal voltage (V)

% Define step size for numerical differentiation


h = 1e-8; % Step size

% Define input voltage


Vi = input('\nEnter the input voltage (Vi): ');

% Initialize Vo
Vo = 0;

% Define the function for diode equation


if Vi >= 0
f = @(Vd) Is*R*(exp(Vd/VT)-1) + Vd + 8 - Vi;
else
f = @(Vd) Is*R*(exp(Vd/VT)-1) + Vd + 6 + Vi;
end

% Perform Newton-Raphson iteration


Tol = 1e-8; % Tolerance
fDeriv = (f(Vo+h) - f(Vo))/h; % Derivative
dx = -f(Vo)/fDeriv; % Change in x
x = Vo + dx; % Update x

% Iterate until convergence


while abs(dx) > Tol
f_deriv = (f(x+h) - f(x))/h;
dx = -f(x)/f_deriv;
x = x + dx;
end

% Compute Vo based on the obtained Vd


Vd = x;
if Vi >= 0
Vo = Vd + 8;
else
Vo = -(Vd + 6);
end

% Display the result


fprintf('\nValue of Vo for given Vi: %.2fV\n\n\n\n', Vo);

Problem 3(c):

clc;
close all;

v = -10:1:10;
Is = 0.1e-12;
Vt = 25e-3;
Vd = 0.7;
R = 5e3;

Id = Is * (exp(Vd/Vt) - 1);

Vi_p = 0:0.001:10;clc;
close all;

% Given parameters
v = -10:1:10;
Is = 0.1e-12;
Vt = 25e-3;
Vd = 0.7;
R = 5e3;

% Diode current calculation


Id = Is * (exp(Vd/Vt) - 1);

% Positive input voltages and corresponding diode voltages


Vi_p = 0:0.001:10;
Vd_1 = zeros(1, length(Vi_p));

% Negative input voltages and corresponding diode voltages


Vi_n = 0:-0.001:-10;
Vd_2 = zeros(1, length(Vi_n));

% Loop for positive input voltages


for i = 1:length(Vi_p)
fv = @(v) Is * R * (exp(v/Vt) - 1) + v + 8 - Vi_p(i);
der = @(v) Is * R * (exp(v/Vt))/Vt + 1;
v0 = 0;

% Newton-Raphson method for solving the equation


v_new = v0 - (fv(v0) / der(v0));
tol = 1e-4;

while (abs(v0 - v_new) > tol)


v0 = v_new;
v_new = v0 - (fv(v0) / der(v0));
end

Vd_1(i) = v_new + 8;
end

% Plot positive input voltages and corresponding diode voltages


plot(Vi_p, Vd_1,'LineWidth',2);
xlabel('Input Voltage (Vi)');
ylabel('Output Voltage (Vd)');
title('Output Voltage vs Input Voltage');
hold on;

% Loop for negative input voltages


for i = 1:length(Vi_n)
fv = @(v1) Is * R * (exp(v1/Vt) - 1) + v1 + 6 + Vi_n(i);
der = @(v1) Is * R * (exp(v1/Vt))/Vt + 1;
v01 = 0;

% Newton-Raphson method for solving the equation


v_new1 = v01 - (fv(v01) / der(v01));
tol = 1e-4;

while (abs(v01 - v_new1) > tol)


v01 = v_new1;
v_new1 = v01 - (fv(v01) / der(v01));
end

Vd_2(i) = v_new1 ;
end

% Calculate the output voltage for negative input voltages and adjust the offset
V_d = -Vd_2 - 6;

% Plot negative input voltages and corresponding diode voltages


plot(Vi_n, V_d,'LineWidth',2);

% Annotations
text(5, 2, 'Vi > 0', 'FontSize', 12); % Annotation for Vi > 0
text(-5, -4, 'Vi < 0', 'FontSize', 12); % Annotation for Vi < 0
text(-7, -6, 'V_d', 'FontSize', 12); % Annotation for the line V_d

legend('Vi > 0', 'Vi < 0'); % Add legend


grid on; % Add grid
hold off; % Release the hold on the plot

Vd_1 = zeros(1, length(Vi_p));


Vi_n = 0:-0.001:-10;
Vd_2 = zeros(1, length(Vi_n));

for i = 1:length(Vi_p)
fv = @(v) Is * R * (exp(v/Vt) - 1) + v + 8 - Vi_p(i);
der = @(v) Is * R * (exp(v/Vt))/Vt + 1;
v0 = 0;

v_new = v0 - (fv(v0) / der(v0));


tol = 1e-4;

while (abs(v0 - v_new) > tol)


v0 = v_new;
v_new = v0 - (fv(v0) / der(v0));
end

Vd_1(i) = v_new + 8;
end

plot(Vi_p, Vd_1,'LineWidth',2);
xlabel('Input Voltage (Vi)');
ylabel('Output Voltage (Vd)');
title('Output Voltage vs Input Voltage');
hold on;
for i = 1:length(Vi_n)
fv = @(v1) Is * R * (exp(v1/Vt) - 1) + v1 + 6 + Vi_n(i);
der = @(v1) Is * R * (exp(v1/Vt))/Vt + 1;
v01 = 0;

v_new1 = v01 - (fv(v01) / der(v01));


tol = 1e-4;

while (abs(v01 - v_new1) > tol)


v01 = v_new1;
v_new1 = v01 - (fv(v01) / der(v01));
end

Vd_2(i) = v_new1 ;
end
V_d=-Vd_2-6;

plot(Vi_n, V_d,'LineWidth',2);
Problem 4:

clc;
close all;

f1 = @(x1,x2) x1^2+x2^2-10;
f2 = @(x1,x2) x1-x2+2;

f1_der_x1 = @(x1,x2) 2*x1;


f1_der_x2 = @(x1,x2) 2*x2;
f2_der_x1 = @(x1,x2) 1;
f2_der_x2 = @(x1,x2) -1;

tol = 1e-6;
x0 = [0.5; 0.5];

dx = Inf; % Initialize dx to a large value to enter the loop


while norm(dx) > tol

f = [f1(x0(1),x0(2)); f2(x0(1),x0(2))];
k = [f1_der_x1(x0(1),x0(2)), f1_der_x2(x0(1),x0(2)); f2_der_x1(x0(1),x0(2)), f2_der_x2(x0(1),x0(2))];

% Compute the step and update x0


dx = -k\f;
x0 = x0 + dx;

% Update iteration count

end

fprintf("Root:\n");
fprintf("x1 = %g\n", x0(1));
fprintf("x2 = %g\n", x0(2));

You might also like