0% found this document useful (0 votes)
33 views

Script Tutorial: Fact.m

The document contains MATLAB scripts that demonstrate: 1) A function to calculate the factorial of a number. 2) A function to calculate the position and velocity of a falling object over time accounting for air resistance. 3) A script that calculates the real or complex roots of a quadratic equation based on the discriminant.

Uploaded by

heypartygirl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Script Tutorial: Fact.m

The document contains MATLAB scripts that demonstrate: 1) A function to calculate the factorial of a number. 2) A function to calculate the position and velocity of a falling object over time accounting for air resistance. 3) A script that calculates the real or complex roots of a quadratic equation based on the discriminant.

Uploaded by

heypartygirl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Script Tutorial

Fact.m

function [product] = fact(n)
product = 1;
for k = 2:n
product = product * k;
end

falling object.m
function [x,v] = falling_object(g,m,k,t)
% A function to calculate the position and velocity of
% a falling object with air resistance proportional to velocity.
% It is assumed that the initial position is 0 with positive
% direction downward and that the initial velocity is also 0
%
% Input:
% g: the acceleration due to gravity (for example 9.81 m/s^2)
% m: the mass of the object (for example, 10 kg)
% k: the drag coefficient (for example, 2 kg/s
% t: the time in seconds since object was dropped
% Output:
% x: the position at time t
% v: the velocity at time t

c1 = m*g/k;
c2 = m/k;

% Initial conditions at time 0

x0 = 0;
v0 = 0;

% Formulas for velocity and position aat time t

v = c1 + (v0 - c1)*exp(-t/c2);
x = x0 + c1*t + c2*(v0 - c1)*(1 - exp(-t/c2));
end

quad script.m
% Finding the roots of a quadratic equation
% ax^2 + bx + c = 0

a = input('Enter coeff of x^2 ');
b = input('Enter coeff of x ');
c = input('Enter constant coeff ');

d = b*b - 4*a*c;

if d > 0
root1 = (-b + sqrt(d)) / (2*a);
root2 = (-b - sqrt(d)) / (2*a);
disp('Real unequal roots');
disp(root1);
disp(root2);
elseif d < 0
root_real = -b / (2*a);
root_imag = sqrt(abs(d));
disp('Complex roots');
disp(complex(root_real, root_imag));
disp(complex(root_real, -root_imag));
else
root = -b / (2*a);
disp('Real equal roots');
disp(root);
end

sq root1.m
% Simple algorithm for square roots

a = input('Number to find square root of: ');
max_iter = input('Maximum iterations: ');

x = a/2;
for k = 1:max_iter
x = 0.5*(x + a/x);
disp(x);
end

sq root2.m
% Simple algorithm for square roots
% This version uses a while loop

a = input('Number to find square root of: ');
max_iter = input('Maximum iterations: ');

x = a/2;
k = 1;
while k <= max_iter
x = 0.5*(x + a/x);
disp(x);
k = k + 1; % don't forget this
end




sq root3.m
% Simple algorithm for square roots
% This version uses a for loop with a break

a = input('Number to find square root of: ');
max_iter = input('Maximum iterations: ');
tol = input('Enter tolerance: ');

x_old = a/2;

for k = 1:max_iter

x_new = 0.5*(x_old + a/x_old);
disp(x_new);

if abs(x_new - x_old) < x_new*tol
break;
end

x_old = x_new;
end

if k == max_iter
fprintf('Failure to converge in %d iterations\n', max_iter)
end

You might also like