In: Computer Science
Solve using matlab code!!
Use the graphical method to solve
4x1 − 8x2 = −24
x1 + 6x2 = 34
Check your results by substituting them back into the equations.(include this on the code)
%4x1 − 8x2 = −24 % These are the two given equations
%x1 + 6x2 = 34
x1_line1=0:0.1:10; % Generating points for axis x1 for line 1
x1_line2=0:0.1:10; % Generating points for axis x1 for line 2
x2_line1 = (1/4) .* (-24 + 8.*x1_line1); % Generating corresponding points of axis x2 for all points in axis x1 of line 1
x2_line2 = 34 - (6.*x1_line2); % Generating corresponding points of axis x2 for all points in axis x1 of line 2
plot(x2_line1, x1_line1,'r') % plotting first line in red and second line in blue colour
hold on
plot(x2_line2, x1_line2,'b');
grid minor % using minor so that we can know the exact intersection point
xlabel('x1')
ylabel('x2')
The above is the code to visualize the two lines. The output of the above code is shown below.
From this, we can clearly see that both lines are intersecting at (4,5).
So, test whether the point (4,5) is the solution. We should check whether that point satisfies both the lines which is done by below code. The output of the below code prints both the statements implying that the point (4,5) is indeed a solution to the given two linear equations
% 4x1−8x2+24 = 0 % These are the two given equations re arranged all terms to ne side
% x1+6x2-34 = 0
res1 = 4*(4)-8*(5)+24;
res2 = (4)+6*(5)-34;
if res1 == 0
disp("Line 1 is satisfied with the point (4,5)")
end
if res2 == 0
disp("Line 2 is satisfied with the point (4,5)")
end