0% found this document useful (0 votes)
55 views12 pages

Assignment 2

The document contains 6 math and engineering problems involving linear algebra, data fitting, and circuit analysis. Problem 1 involves solving a system of linear equations. Problem 2 deals with projectile motion for varying angles. Problem 3 examines complex functions. Problem 4 performs polynomial regression on sample data. Problem 5 analyzes current-voltage characteristics of a diode. Problem 6 finds coefficients of a polynomial that fits given x-y data points.

Uploaded by

Ramima Mumtarin
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)
55 views12 pages

Assignment 2

The document contains 6 math and engineering problems involving linear algebra, data fitting, and circuit analysis. Problem 1 involves solving a system of linear equations. Problem 2 deals with projectile motion for varying angles. Problem 3 examines complex functions. Problem 4 performs polynomial regression on sample data. Problem 5 analyzes current-voltage characteristics of a diode. Problem 6 finds coefficients of a polynomial that fits given x-y data points.

Uploaded by

Ramima Mumtarin
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

Problem 1

clc; clear all; close all;

coeff=[2.5 -1 3 1.5 -2;...


3 4 -2 2.5 -1;...
-4 3 1 -6 2;...
2 3 1 -2.5 4;...
1 2 5 -3 4];
V=[57.1;27.6;-81.2;-22.2;-12.2];
solution=inv(coeff)*V;
a=solution(1,1)
b=solution(2,1)
c=solution(3,1)
d=solution(4,1)
e=solution(5,1)

Problem 2

clc;
clear all;
close all;

theta=[5 30 45 60 85];
x=0:1:300;
figure;
hold on;

for i=1:length(theta)
y=x*tand(theta(i))-0.00196*(x.^2)/(cosd(theta(i))^2);
plot(x,y,'LineWidth',2);
end

title('Plot for Different Theta Values','FontSize',15);


xlabel('x','FontSize',15);
ylabel('y','FontSize',15);
legend(arrayfun(@(i)...
sprintf('Theta= %d',theta(i)),1:length(theta),...
'UniformOutput',false),'FontSize',12);
grid on;

xlim([0,max(x)]);
ylim([0,130]);

% 2nd Portion

theta_2=linspace(0, 90, 100);


all_y_2=zeros(length(x),length(theta_2));

for i = 1:length(theta_2)
y=x*tand(theta_2(i))-
0.00196*(x.^2)/(cosd(theta_2(i))^2);
all_y_2(:, i) = y;
end

H_max=max(all_y_2);
figure;
plot(theta_2,H_max,'LineWidth',2);
title('Maximum Height vs. Theta','FontSize',15);
xlabel('Theta', 'FontSize',15);
ylabel('Maximum Height','FontSize',15);
grid on;
Problem 3
clc;
clear all;
close all;

theta = 0:.01*pi:2*pi;
x = cos(theta);
y = sin(theta);

figure;

% Create a figure with subplots


subplot(1,2,1);

% Plot the input contour


z = x + 1i * y;
plot(real(z), imag(z), 'LineWidth', 2);
xlabel('Re(z)');
ylabel('Im(z)');
title('z = exp(i\theta)','FontSize',15);
grid on;
axis equal;
% Plot the output contour
subplot(1,2,2);
f = z.^2;
plot(real(f), imag(f), 'LineWidth', 2);
xlabel('Re(z^2)');
ylabel('Im(z^2)');
title('f = z^2','FontSize',15);
grid on;
axis equal;
Problem 4

clc;
clear all;
close all;

insert=load("data_p3.mat");
x=insert.x;
y=insert.y;

% Plotting the original data


plot(x, y, 'ok');
xlabel('x');
ylabel('y');
legend('Points');
title('Points','FontSize',15);

fprintf('My hypothesis: Polynomial Regression\n');

% Linear Regression
A=[sum(x.^2),sum(x);sum(x),2];
v=[sum(x.*y);sum(y)];
solution=inv(A)*v;
a1=solution(1,1);
a0=solution(2,1);
mdl_linear=fitlm(x, y);

% R-squared for linear regression


rsquared=mdl_linear.Rsquared.Ordinary;
disp(['R-squared Linear regression: ', num2str(rsquared)]);

% 2nd order polynomial regression


degree=2;

A2=[sum(x.^4), sum(x.^3), sum(x.^2);...


sum(x.^3), sum(x.^2), sum(x);...
sum(x.^2), sum(x), numel(x)];
V2=[sum((x.^2).*y); sum(x.*y); sum(y)];
solution_2=inv(A2)*V2;
y1=solution_2(1)*(x.^2)+solution_2(2)*x+solution_2(3);
coefficients=polyfit(x, y, degree);
% Fit polynomial model using fitlm
mdl_poly = fitlm(x,y,'poly2');

% R-squared for polynomial regression


rsquared_poly=mdl_poly.Rsquared.Ordinary;
disp(['R-squared Polynomial regression: ',
num2str(rsquared_poly)]);
x_values=linspace(min(x),max(x),100);
% Evaluate the polynomial at the new x values
y_predicted_poly=polyval(coefficients, x_values);

% Plotting with fitting


figure;
subplot(1,2,1);
plot(x,a0+a1*x,'LineWidth',2);
xlabel('x');
ylabel('y');
legend('Linear');
title('Linear Regression ','FontSize',15);
hold on;
plot(x, y, 'ok');
xlabel('x');
ylabel('y');
legend('Points');
title('Points','FontSize',15);

subplot(1,2,2);
plot(x, y, 'ok');
hold on;
plot(x_values, y_predicted_poly, 'r-', 'LineWidth', 2,
'DisplayName', 'Polynomial Fit');
hold off;

title('Polynomial Regression with fitting','FontSize',15);


xlabel('X');
ylabel('Y');
legend('show');
grid on;

% Comparison of R-squared values


if rsquared_poly>rsquared
fprintf('Polynomial Regression (degree %d)!!!\n',
degree);
fprintf('My hypothesis was correct\n');
else
fprintf('Linear Regression!!!\n');
fprintf('Alas! I was wrong. My whole life was a lie\n');
end
Problem 5
clc;clear all;close all;

% Constants
e=1.602176634e-19;
kB=1.380649e-23;
T=300;
Vt=kB*T/e;

% Data
V=0.1:0.1:1;
I=[8.35 22.3 48.9 97.2 183.2 321.4 538.85 868.05 1365
2052.5] * 1e-9;

% Linearization
m=1-exp(-V/(Vt));
y=log(I./m);
x=V;

% Matrix Declaration
coeff=[length(x),sum(x);sum(x),sum(x.^2)];
Value=[sum(y);sum(x.*y)];
solution=inv(coeff)*Value;

% Values of a and b
b=solution(1);
a=solution(2);

% Values of Is and n from the data


Is=exp(b);
n=(Vt*a)^-1;

% Plotting
plot(x, exp(a*x+b), 'LineWidth', 2);
xlabel('V');
ylabel('log(I)');
title('I-V curve');
legend('Best fitted line');
hold on;
plot(x,exp(y),'ok')
legend('points');
% Display parameters
fprintf('Is: %.4e\n', Is);
fprintf('n: %.4f\n', n);

Problem 6
clc; clear all; close all;

m=input('Enter the order of the polynomial ');

x=input('Enter the data for x first use 3rd bracket [] ');


y=input('Enter the data for y first use 3rd bracket [] ');
a=ones(m+1,1);
a=coeff_in(x,y,m);

function coeff=coeff_in(x,y,m)
coeff=ones(m+1);
V=ones(m+1,1);
n=2*(m);
for i=m+1:-1:1
k=m+1;
while k:-1:1
coeff(i,k)=sum(x.^n);
k=k-1;
n=n-1;
end
n=n+m;
coeff(1,1)=length(x);
end

for i=1:m+1
V(i,1)=sum((x.^(i-1)).*y);
end
solution=inv(coeff)*V
%this function will return the values of the coefficients
end

You might also like