-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearSystemSolver.m
62 lines (47 loc) · 1.71 KB
/
linearSystemSolver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%This function solves linear systems equations, asks for each parameter,
%one at a time
%Made by: Guilherme Elias
function linearSystemSolver()
clear
clc;
equations = input('Type in the number of equations: ');
first = 'st';
second = 'nd';
third = 'rd';
fourthelse = 'th';
%% Making the matrix for the coefficients given
%Goes through the rows to make the matrix
for eq = 1:equations
%Goes through the columns in the rows
for var = 1:equations
%Shows the message for the user with the correct number of
%equation and coefficient
fprintf('Enter the coefficient %d of equation %d: ', var, eq);
%Stores the value input by the user for the equation matrix a
a(eq, var) = input('');
end
%% Organising Results
%If statement to print st, nd, rd, th, after the number asked
if eq == 1
fprintf('Enter the %d%s constant: ', eq, first);
elseif eq == 2
fprintf('Enter the %d%s constant: ', eq, second);
elseif eq == 3
fprintf('Enter the %d%s constant: ', eq, third);
else
fprintf('Enter the %d%s constant: ', eq, fourthelse);
end
%Stores the constant in the matrix b (column matrix)
b(eq, 1) = input('');
end
%% Printing answers in a cute way
%Gets the number of rows and columns of matrix a
[rows, cols] = size(a);
for i = 1:rows
%Calculates the value of x
x = inv(a)*b;
%Prints the roots ordered
fprintf('\n');
disp(['x', num2str(i), ' = ', num2str(x(i))]);
end
end