In: Electrical Engineering
****MATLAB QUESTION**** As an enthusiastic and motivated student you decide to go out and buy plenty of pens for all your classes this semester. This spending spree, unfortunately, occurred before you realized your engineering classes seldom require the use of "ink". So now, you're left with four different types of pens and no receipt-you only remember the total amount you spent and not the price of each type of pen. You decide to get together with three of your friends who coincidentally did the same thing as you, buying the same four types of pens and knowing only the total amount.
Write a script to find the prices of each type of pen..
Four different types of Pens are : A,B,C,D
We know total amount too. We can solve this by making four equations with four unknowns
And coefficients of our equations are number of pens we bought.
a A + b B + c C + d D = Z
This equation represents buying a pens of Type A , b pens of Type B, c pens of Type C and d pens of Type D and
Z is total amount.( of one person)
We have four persons , so we get four equations
a1 A + b1 B + c1 C + d1 D = Z1
a2 A + b2 B + c2 C + d2 D = Z2
a3 A + b3 B + c3 C + d3 D = Z3
a4 A + b4 B + c4 C + d4 D = Z4
Now we solve these equations in matlab .
CODE :
OUTPUT :
RAW_CODE :
%take this example equations
% change coeffiecients and final amount with your samples
% 2A + 1B +0C + 6D = 64
% 5A + 2B +0C + 0D = 37
% 0A + 7B +2C + 2D = 66
% 0A + 0B +8C + 9D = 104
coefff_matrix = [2,1,0,6; 5,2,0,0; 0,7,2,2; 0,0,8,9];
final_amount = [64; 37; 66; 104];
% solving equations
X = inv(coefff_matrix)*final_amount;
% we can also use X = coefff_matrix \ final_amount
fprintf('price of type1 pen : %f\n',X(1));
fprintf('price of type2 pen : %f\n',X(2));
fprintf('price of type3 pen : %f\n',X(3));
fprintf('price of type4 pen : %f\n',X(4));