1/8
Looks like no tags are added yet.
For small engines (2 cycle engines) you need to mix fuel & gas with certain ratio (50 gas: 1 oil). If you fill the gas in gallons and decimal of gallons (e.g. 1.23 gal) and the oil is sold by fluid ounces (fl oz) and you are limited by certain container size to mix the gas and oil (say 1 gal jug). Write a code that asks the user about the ratio (fraction of gas & fraction of oil), the size of the mixing container and outputs the amount of needed gas in gallons and the amount of needed oil in fl oz (1gal = 128 fl oz)
% Ask the user for inputs
jug = input('Enter mixing container size in gallons: ');
gas_r = input('Enter gas ratio part (e.g., 50): ');
oil_r = input('Enter oil ratio part (e.g., 1): ');
% Calculate gas (gal) and oil (fl oz)
tot = gas_r + oil_r;
gas_gal = jug * (gas_r / tot);
oil_oz = jug (oil_r / tot) 128;
% Display output
fprintf('For a %.0f gal jug and %.0f:%.0f gas:oil ratio you need %.4f gal of gas
and %.4f fl oz of oil\n', ...
jug, gas_r, oil_r, gas_gal, oil_oz);
Solve the below sets of equation using matlab matric multiplication, comment the arranged equtions
3x+5z=13
2y+x=-1
5y+z=-3
format compact; clc;clear;
% Arrange equations in the order x, y, z
% Equation 1: 3x + 0y + 5z = 13
% Equation 2: 1x + 2y + 0z = -1
% Equation 3: 0x + 5y + 1z = -3
A = [3 0 5;
1 2 0;
0 5 1];
B = [13;
-1;
-3];
% Solve using matrix multiplications
X=inv(A)*B;
%Display answers
x = X(1)
y = X(2)
z = X(3)
Solve the following set of linear equations using Matrix operations
2 𝑥 − 𝑦 + 11 𝑧 = 5
3𝑥/𝑦 = 7
𝑦 − 3 𝑧 + 9 𝑥 = 11
a. Define the coefficient Matrix A
b. Define the right-hand side column vector b
c. Use the matrix inverse to find the solution for x, y and z. save it to the variable solution.
Use MATLAB to verify the solution using the 2nd equation.
% Problem 1
clear; clc;
% a. Define the coefficient matrix A
A = [ 2, -1, 11;
3, -7, 0;
9, 1, -3 ];
% b. Define the right-hand side column vector b
b = [ 5;
0;
11 ];
% c. Use matrix inverse to find solution vector [x; y; z]
solution = inv(A) * b;
% Extract variables
x = solution(1);
y = solution(2);
z = solution(3);
% Display solution
disp('Solution [x; y; z]:');
disp(solution);
% Verify using the second equation: 3*x/y should equal 7
verification = 3 * x / y;
fprintf('Verification of 2nd equation (3*x/y): %.4f\n', verification);
%Answers
x = solution(1); %{1.2543}%
y = solution(2); %{0.5375}%
z = solution(3); %{0.2754}%
Verification of 2nd equation (3*x/y): 7.0000
Write a code that plots the following functions in an array of 3 by 1 plots (i.e one column of
subplots) in ONE figure according to the following:
𝑦1 = 𝑒0.15 𝑥 𝑠𝑖𝑛(2𝑥3 − 3𝑥) 𝑦2 = 10 + 5 cos(3𝑥) 𝑦3 = 10 + 𝑦1
0.2 − 3𝑦2
Where x takes the values from 0 to 10 with step of 0.05 and any value inside sin or cos is in
rad.
a) In the first subplot, plot y1.
b) In the second subplot, plot y2.
c) In the third subplot, plot y3.
For each of the three plots, add grid, title, axis labels, legend. Save the script AND the
generated figure and upload them to Moodle. Submit BOTH your script file and the figure file wile uploading your HW files.
Problem 2
clear; clc; close all;
% Define x range from 0 to 10 with step 0.05
x = 0:0.05:10;
% Calculate functions using element-wise operations
y1 = exp(0.15 x) . sin(2 x.^3 - 3 x);
y2 = 10 + 5 cos(3 x);
y3 = 10 + (y1 / 0.2) - 3 * y2;
% Create figure with 3x1 subplots
figure;
% First Subplot (y1)
subplot(3, 1, 1);
plot(x, y1, 'b-', 'LineWidth', 1.5);
grid on;
title('Plot of y_1 = e^{0.15x} sin(2x^3 - 3x)');
xlabel('x (rad)');
ylabel('y_1');
legend('y_1');
% Second Subplot (y2)
subplot(3, 1, 2);
plot(x, y2, 'r-', 'LineWidth', 1.5);
grid on;
title('Plot of y_2 = 10 + 5 cos(3x)');
xlabel('x (rad)');
ylabel('y_2');
legend('y_2');
% Third Subplot (y3)
subplot(3, 1, 3);
plot(x, y3, 'g-', 'LineWidth', 1.5);
grid on;
title('Plot of y_3 = 10 + y_1 / 0.2 - 3 y_2');
xlabel('x (rad)');
ylabel('y_3');
legend('y_3');
% Save the generated figure
saveas(gcf, 'LastName_HW_02_pb_02_Figure.png');
Write a script file that calculates the volume and surface area of a cone if its radius is given
as r=7 mm and its height is given as h=15 mm. The surface area of a cone is given by 𝐴 =
𝜋𝑟 𝑙 + 𝜋𝑟2 where 𝑙 = √𝑟2 + h2 and the volume is given by 𝑉 = 1
3 𝜋𝑟2ℎ. Use fprintf
command to display the results with 2 decimal points in a descriptive text and include units
(mm2 and mm3).
% Problem 3
clear; clc;
% Given dimensions
r = 7; % mm
h = 15; % mm
% Calculations
l = sqrt(r^2 + h^2);
A = pi r l + pi * r^2;
V = (1/3) pi r^2 * h;
% Output formatted results with 2 decimal places
fprintf('The surface area of the cone is: %.2f mm^2\n', A);
fprintf('The volume of the cone is: %.2f mm^3\n', V);
%Answers
The surface area of the cone is: %{517.96 mm^2}%
The volume of the cone is: %{769.69 mm^3}%

% Problem 4
clear; clc;
% Input values
W = 6; % meters
A = 80; % square meters
% Calculations
D = W / sqrt(2);
L = (A - 0.5 * D^2) / W;
total_fence_length = W + 2 L + 2 D;
% Display results
fprintf('Required length L: %.2f m\n', L);
fprintf('Total fence length required: %.2f m\n', total_fence_length);
%Answers
Required length L: %{11.83 m}%
Total fence length required: %{38.15 m}%

% Script to prompt user for coefficients, call solveQuadratic, and display results
% 1. Ask user for inputs
a = input('Enter coefficient a: ');
b = input('Enter coefficient b: ');
c = input('Enter coefficient c: ');
% 2. Call the function
[x1, x2] = solveQuadratic(a, b, c);
% 3. Display the results neatly using fprintf
fprintf('\n--- Quadratic Equation Solutions ---\n, x1 = %.2f\n, x2 = %.2f\n', x1,
x2);
% --- Quadratic Equation Solutions ---
% x1 = 1.00
% x2 = 1.00

function[x1, x2] = solveQuadratic (a, b, c)
%solveQuadratic calculates the roots of a quadratic equation ax^2 + bx +
%c=0
%Inputs: a, b, c (coefficients)
%Outputs: x1, x2 (solutions)
%Calculate discriminant
d=sqrt(b^2-4*a*c);
%Calculate the two solutions using the quadratic formula
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end