KEMBAR78
Assignment 1 | PDF | Algorithms | Algebra
0% found this document useful (0 votes)
10 views15 pages

Assignment 1

The document contains a series of MATLAB code snippets for various mathematical and graphical computations. It includes plotting functions, matrix operations, root-finding algorithms, and interpolation techniques. Each section demonstrates a different concept, such as plotting exponential and trigonometric functions, generating random matrices, and calculating trajectory and temperature distributions.

Uploaded by

sachinya144
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)
10 views15 pages

Assignment 1

The document contains a series of MATLAB code snippets for various mathematical and graphical computations. It includes plotting functions, matrix operations, root-finding algorithms, and interpolation techniques. Each section demonstrates a different concept, such as plotting exponential and trigonometric functions, generating random matrices, and calculating trajectory and temperature distributions.

Uploaded by

sachinya144
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/ 15

Q1

x = -2:0.01:2

plot(x, exp(2*x), "r--")

xlabel ("x")

ylabel ("y")

hold on

plot(x, sin(3*x), "b-", LineWidth = 1)

legend("e^{2x}","Sin(3x)")

title('Plots of e^{2x} and sin(3x)')


Q2
a = input('Enter the Number of columns: ');

b = input('Enter the Number of rows: ');

matrix = randi([1, 100], b, a);

disp(matrix)

average = mean(matrix(:))

maximum = max(matrix(:))

minimum = min(matrix(:))

writematrix(matrix, 'matrix.txt')
Q3
f =@(x) x^3 - 5*x + 3

df=@(x) 3*x^2 - 5

x0 = input('Enter initial guess: ');

tol = input('Enter tolerance value: ');

max_iter = 1000;

iter = 0;

error = inf;

while error > tol && iter < max_iter

x1 = x0 - f(x0)/df(x0)

error = abs(x1-x0)

x0 = x1

iter = iter + 1

end

if iter == max_iter
fprintf('Method did not converge within %d iterations.\n', max_iter)

else

fprintf('The root is approximately: %.6f\n', x1)

fprintf('Number of iterations: %d\n', iter)

end

Q4
x = [1,2,3,4,5]
y = [2,4,8,16,32]

n = length(x)

diff_table = zeros(n, n);

diff_table(:,1) = y

for j = 2:n

for i = 1:(n-j+1)

diff_table(i,j) = diff_table(i+1,j-1) - diff_table(i,j-1);

end

end

disp('Forward Difference Table:')

fprintf('x\t\t');

for j = 1:n

fprintf('Δ^%d y\t\t', j-1);

end

fprintf('\n');

for i = 1:n

fprintf('%.0f\t\t', x(i));

for j = 1:(n - i + 1)

fprintf('%.2f\t\t', diff_table(i,j));

end

fprintf('\n');

end
Q5
t = 0:0.1:10

v = 20*(1-exp(-0.5*t))

plot(t,v,LineWidth = 2)

xlabel ("Time t")


ylabel ("Velocity v")

v1 = max(v)

v2 = 0.95*v1

t_interp = interp1(v, t, v2)

fprintf('Interpolated time when v reaches %.2f is t = %.4f seconds\n',


v2, t_interp)

t1 = -2*log(1 - 0.05*v2)
Q6
a = zeros(3)

p = input('Enter First Diagonal Element: ');

q = input('Enter Second Diagonal Element: ');

r = input('Enter Third Diagonal Element: ');

a(1,1) = p;

a(2,2) = q;

a(3,3) = r;

disp(a)

Q7
f = @(x) cos(x) - x

a = input("Enter Left Boundary ")

b = input("Enter Right Boundary ")

tol =input("Enter Tolerance Error ")

max_iter = 1000

if f(a)*f(b) > 0

error('No sign change in interval. Choose a different [a,b].');


end

iter = 0

error = inf

while error > tol && iter < max_iter

c = (a + b)/2

fc = f(c)

if f(a) * fc < 0

b = c;

else

a = c;

end

error = abs(b - a);

iter = iter + 1;

end

fprintf('Root is approximately: %.6f\n', c);

fprintf('Number of iterations: %d\n', iter);


Q8
u = 30

theta_deg = 45

theta_rad = deg2rad(theta_deg)
g = 9.81

t2 = 2*u*sin(theta_rad)/g

t = linspace(0, t2, 100)

x = u*cos(theta_rad)*t

y = u*sin(theta_rad)*t - 0.5*g*t.^2

plot(x,y,LineWidth=2)

xlabel ("Distance X")

yLabel ("Distance Y")

title ("Trajectory")
Q9
x = [0 1 2 3];

y = [1 2.7 7.4 20.1]

x_point = 1.5

y_interp = interp1(x, y, x_point, 'linear')

fprintf('Estimated value of y at x = %.1f is %.4f\n', x_point, y_interp)

plot(x, y, 'o-', 'LineWidth', 2);

hold on

plot(x_point, y_interp, 'rx', 'MarkerSize', 10, 'LineWidth', 2); %


Interpolated point
xlabel('x')

ylabel('y')

title('Linear Interpolation')

legend('Data Points', 'Interpolated Value', 'Location', 'NorthWest')

Q10
l = 1

x = linspace(0, l, 100)

T = 100 * sin(pi * x)
plot(x, T, 'r', 'LineWidth', 2)

xlabel('Position along the rod (m)')

ylabel('Temperature (°C)');

title('Steady-State Temperature Distribution along the Rod')

avg_T = trapz(x, T) / l

max_T = max(T)

fprintf('Average Temperature = %.2f °C\n', avg_T)

fprintf('Maximum Temperature = %.2f °C\n', max_T)

You might also like