Assignment 2
Assignment 2
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
xlim([0,max(x)]);
ylim([0,130]);
% 2nd Portion
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;
clc;
clear all;
close all;
insert=load("data_p3.mat");
x=insert.x;
y=insert.y;
% 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);
subplot(1,2,2);
plot(x, y, 'ok');
hold on;
plot(x_values, y_predicted_poly, 'r-', 'LineWidth', 2,
'DisplayName', 'Polynomial Fit');
hold off;
% 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);
% 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;
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