In: Mechanical Engineering
% Consider the following system of equations:
% -2a +5b + c + 3d + 4e - f = 0
% 2a - b - 5c - 2d + 6e + 4f = 1
% -a + 6b - 4c - 5d + 3e - f = -6
% 4a + 3b - 6c - 5d - 2e - 2f = 10
% -3a + 6b + 4c + 2d - 6e + 4f = -6
% 2a + 4b + 4c + 4d + 5e - 4f = -2
% a)
% Define a symbolic variable for each of the equation and use
symbolic
% capability to solve for each unknown. Convert your results to
doubles.
% Answer
% b)
% Compare the amount of time it takes to solve the system of
equations
% using left division and symbolic math by using the tic and toc
function.
% Answer
a) Matlab Code:
clc;
clear;
tic
syms a b c d e f
%A*B=D
A=[-2,5,1,3,4,-1;2,-1,-5,-2,6,4;-1,6,-4,-5,3,-1;4,3,-6,-5,-2,-2;-3,6,4,2,-6,4;2,4,4,4,5,-4];
D=[0;1;-6;10;-6;-2];
B=[a;b;c;d;e;f];
sol = solve(A*B==D,a,b,c,d,e,f);
double B;
B=[sol.a;sol.b;sol.c;sol.d;sol.e;sol.f]
timerVal = toc
Outbut:
b) above image shows
time taken for a) is 0.4679 sec,
using left division method:
Matlab Code:
clc;
clear;
tic
A=[-2,5,1,3,4,-1;2,-1,-5,-2,6,4;-1,6,-4,-5,3,-1;4,3,-6,-5,-2,-2;-3,6,4,2,-6,4;2,4,4,4,5,-4];
D=[0;1;-6;10;-6;-2];
double X;
X = A\D
timerVal = toc
Output:
Time for left division is very small compared to first part
time = 5.8415e-04 sec,