Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function practice1()
- clear;clc
- fprintf('Mustafa Kalash\nMATLAB Practice 1\n')
- fprintf('\nProblem 1\n')
- m = input('What is the upper bound of the summation?\nm = ');
- sigma = 0;
- for n = 0:m
- sigma = sigma + ((-1 / 3) ^ n) / (2 * n + 1);
- end
- answer = sqrt(12) * sigma;
- fprintf('The answer is %.2f.\n', answer)
- n = 0:3;
- answer = sqrt(12) * sum(((-1 / 3) .^ n) ./ (2 * n + 1));
- fprintf('The answer is %.2f.\n', answer)
- fprintf('\nProblem 2\n')
- cases = input('How many cases?\ncases = ');
- for n = 1:cases
- a = input('What is a?\na = ');
- b = input('What is b?\nb = ');
- c = input('What is c?\nc = ');
- D = b^2 - 4 * a * c;
- if D > 0
- rootOne = (-b + sqrt(D)) / (2 * a);
- rootTwo = (-b - sqrt(D)) / (2 * a);
- fprintf('The equation has two roots: %.2f, %.2f.\n', rootOne, rootTwo)
- elseif D == 0
- root = -b / (2 * a);
- fprintf('The equation has one root: %.2f.\n', root)
- else
- fprintf('The equation has no real roots.\n')
- end
- end
- % Problem 3
- Data = reshape((randi(87, 24)), 576, 1);
- divisableBy2 = 0;
- data = zeros(1, 7);
- for i = 1:length(Data)
- n = Data(i);
- if ~rem(n, 2)
- divisableBy2 = divisableBy2 + 1;
- end
- if n >= 0 && n < 15
- data(1) = data(1) + 1;
- elseif n >= 15 && n < 30
- data(2) = data(2) + 1;
- elseif n >= 30 && n < 45
- data(3) = data(3) + 1;
- elseif n >= 45 && n < 60
- data(4) = data(4) + 1;
- elseif n >= 60 && n < 75
- data(5) = data(5) + 1;
- elseif n >= 75 && n < 90
- data(6) = data(6) + 1;
- elseif n >= 90
- data(7) = data(7) + 1;
- end
- end
- figure1 = figure;
- axes1 = axes('Parent', figure1, 'XTickLabel', {'0-15', '15-30', '30-45', '45-60', '60-75', '75-90', '90+'}, 'XTick', 1:7);
- box(axes1, 'on')
- hold(axes1, 'on')
- bar(data)
- fprintf('\nProblem 4\n')
- n = input('How many numbers do you want to calculate?\nn = ');
- for i = 1:n
- fprintf('What is number %i?\n', i)
- m = input('m = ');
- fprintf('%.2f! = %.2f\n', m, calcFactorial(m));
- end
- fprintf('\nProblem 5\n')
- i = 1;
- while 1
- x = input('Enter the next value.\nx = ');
- if x >= 1
- a = input('Illegal value entered. Retry? (1 / 0)\n');
- if a
- continue
- else
- break
- end
- end
- y(i) = log(1/(1 - x));
- i = i + 1;
- end
- fprintf('y = ')
- fprintf('%.2f, ', y(1:length(y) - 1))
- fprintf('%.2f.\n', y(length(y)))
- end
- function fact = calcFactorial(n)
- if n < 0 || rem(n, 1) ~= 0
- error('Error: n must be a positive integer.')
- elseif n == 0
- fact = 1;
- else
- fact = n .* calcFactorial(n - 1);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement