You are on page 1of 10

Part B

Question 5
% Problem 5

% Clear the workspace and command window


clear;
clc;

% Define the equation for 'L'


fun = @(x) 100*((x/0.6).^0.625).*((1-x)/0.4).^(-1.625)-70; %Formula for 'L',
(L=70) % Equation for 'L' where L = 70

% Solve for 'x' using fsolve to find the value of 'x' that satisfies the equation
SolutionX = fsolve(fun, 0); % Solving to find the value of 'x'

fprintf('SolutionX: %.4f\n', SolutionX);

% Plot the equation and add 70 to it (to match the L=70 condition)
x_values = 0:0.01:0.6; % Define x values for the plot
plot(x_values, fun(x_values) + 70); % Plot L vs. x
grid on
title('L vs x') % Title of the graph
xlabel('x [mol B/mol]') % Label for the x-axis
ylabel('x [mol]') % Label for the y-axis

% Plot the equation and add 70 to it (to match the L=70 condition)
x_values = 0:0.01:0.6; % Define x values for the plot
y_values = fun(x_values) + 70; % Calculate corresponding y-values

% Find the corresponding x-value at y=70


y_desired = 70; % The desired y-value
x_corresponding = interp1(y_values, x_values, y_desired); % Interpolate to find
the corresponding x-value

% Print the corresponding x-value


fprintf('At L=%.2f in graphical solutions, corresponding x value is %.4f\n',
y_desired, x_corresponding);
% Calculate the percentage error by comparing the computed 'x' with the selected
'x' from the graph
error = abs((SolutionX - x_corresponding) / SolutionX) * 100;
fprintf('Percentage error: %f\n\n', error) % Display the percentage error

Conclusions:
In this MATLAB code, a numerical solution is obtained for the equation
representing a chemical process. The code successfully finds the corresponding
x-value for a given y-value, allowing for the precise determination of the
chemical reaction's state under specific conditions.
Question 6

% Problem 6

% Define the initial temperature and coordinates


T0 = 80; % Initial temperature (constant)
x0 = 1; % x-coordinate (constant)
y0 = 1; % y-coordinate (constant)

% Define the temperature distribution function


T = @(x,y) T0*exp(-(x-x0).^2).*exp(-3*(y-y0).^2);

% Define the range of x and y values

x = linspace(0,1,100);
y = linspace(0,1,100);

% Create a grid of x and y values


[X,Y] = meshgrid(x,y);

% Evaluate the temperature distribution at each point in the grid


Z = T(X,Y);

% Create a surface plot


figure;

surf(X,Y,Z);

xlabel('x');

ylabel('y');

zlabel('Temperature');

title('Surface plot of temperature distribution');

% Create a contour plot

figure;

contour(X,Y,Z);

xlabel('x');

ylabel('y');

colorbar;

title('Contour plot of temperature distribution');

% Calculate the temperature at the corner (x=0, y=0)


corner_temp = T(0,0);
fprintf('Temperature at the corner (x=0, y=0) is %f\n', corner_temp);
Conclusions:
In this MATLAB code, a temperature distribution model is defined based on
specified initial conditions and coordinates. The code then generates visual
representations of this distribution through both a surface plot and a contour
plot. Additionally, it calculates and displays the temperature at the corner point
(x=0, y=0) within the defined area.
Question 7
% Read data from the 'P7.csv' file
M = dlmread('P7.csv');

% Extract force and distance data


F = M(1, :);
D = M(2, :);

% Calculate work done for each segment


workDone = F .* D;

% Add work done as the third row to the matrix


M(3, :) = workDone;

% Calculate the total work done


totalWork = sum(M(3, :));

% Print results
fprintf('Work done in each segment: ');
disp(workDone);
fprintf('Total work done: %.2f\n', totalWork);

% Create bar and pie diagrams


figure;

% Bar diagram
subplot(2, 1, 1);
bar(workDone);
title('Work Done in Each Segment');
xlabel('Segment');
ylabel('Work Done');
xticklabels({'1', '2', '3', '4', '5'});

% Pie diagram
subplot(2, 1, 2);
pie(workDone, {'Segment 1', 'Segment 2', 'Segment 3', 'Segment 4', 'Segment 5'});

% Save the matrix as a CSV file


segment_1 = M(:, 1);
segment_2 = M(:, 2);
segment_3 = M(:, 3);
segment_4 = M(:, 4);
segment_5 = M(:, 5);
rows = ["force"; "distance"; "work"];
T = table(segment_1, segment_2, segment_3, segment_4, segment_5, 'RowNames', rows)

% Write the table to a CSV file


writetable(T, 'table.csv');

Conclusions:
In this MATLAB code, data from 'P7.csv' was read and processed to calculate
the work done for each of the five path segments, with the results added as
the third row of the matrix 'M'. The total work done over the entire path was
computed, and visualizations in the form of bar and pie diagrams were created
to illustrate work distribution among segments. Additionally, the data was
structured into a table and saved as 'table.csv' for further analysis and
reference.
Question 8
clc;
clear all;
close all;

% Ask the user for the value of A


A = input('Enter the value of A: ');

C = 40;
S = 30;

% Define the area equation


% (pi/2)*R^2 + (L*2)R - A = 0

% Define the functions for R and cost


R = @(L) (-2 * L + sqrt(4 * L .* L + (2 * pi * A))) / pi;
cost = @(L) ((2 * R(L) + 2 * L) * S) + (pi * R(L) * C);

% Define the optimization options


options = optimset('TolX', 0.01); % Set resolution to 0.01 m

% Use fminsearch to find the minimum cost


% Start with an initial guess of 0
L = fminsearch(cost, [0], options);

fprintf('For minimum cost:\n');


disp('L =');
disp(L);
disp('R =');
disp(R(L));

disp('Minimum cost =');


disp(cost(L));

Conclusions:
The code provided calculates the values of R and L that minimize the total cost
of a fenced enclosure while ensuring a given area constraint A. It utilizes the
fminsearch optimization function with a resolution of 0.01 meters. The results
are then displayed, including the minimum length (L), the corresponding radius
(R), and the minimum cost.
Question 9
Part a)
syms r V A

% Given equations for V and A


eq1 = V == (1/3) * pi * r^2 * h;
eq2 = A == pi * r * sqrt(r^2 + h^2);

% Solve eq1 for h


h_eq = solve(eq1, h);

% Substitute the expression for h in eq2


eq2_subs = subs(eq2, h, h_eq);

% Solve eq2_subs for A in terms of r and V


A_expr = solve(eq2_subs, A);

% Simplify the expression


A_expr_simplified = simplify(A_expr);

disp('a. Expression for A as a function of r and V:');


disp(A_expr_simplified);

Part b)

function A = computeA(R)
global V

% Calculate A using the provided equation


A = pi * R * sqrt(R^2 + (3 * V / (pi * R^2))^2);
end

Part C)
global V
V = 10; % Set the value of V

% Define the function for optimization


fun = @(R) computeA(R);

% Define the range for 'r'


r_min = 0.1; % Minimum value for 'r'
r_max = 10; % Maximum value for 'r'

% Find the optimal 'r' using fminbnd


optimal_r = fminbnd(fun, r_min, r_max);

% Calculate the corresponding value of 'h'


optimal_h = (3 * V) / (pi * optimal_r^2);

% Plot V versus r to investigate sensitivity


r_values = linspace(0.1, 10, 100);
A_values = arrayfun(fun, r_values);

% Find the range of 'r' values that increases 'A' by 10%


upper_bound = 1.1 * min(A_values(A_values > 0));

% Plot V versus r
figure;
plot(r_values, A_values);
xlabel('r (cm)');
ylabel('A');
title('Paper Cup Area vs. r');
grid on;

fprintf('c. Optimal r: %.2f cm\n', optimal_r);


fprintf(' Corresponding h: %.2f cm\n', optimal_h);
fprintf(' Range of r for A < 1.1 * A_min: 0.1 cm to %.2f cm\n',
r_values(A_values > upper_bound));

Conclusions:
In conclusion, we have successfully addressed the given tasks and questions
related to the conical paper cup problem. Here are the key findings:
a. We derived an expression for the paper surface area 'A' as a function of the
base radius 'r' and volume 'V' by eliminating the height 'h' from the equations.
b. We created a user-defined function that computes 'A' based on a given 'r'
and the global value of 'V'.
c. Using the 'fminbnd' function, we found the optimal 'r' that minimizes 'A' for
'V = 10 cm^3'. The corresponding 'h' was calculated as well. We also
investigated the sensitivity of the solution by plotting 'V' versus 'r' and
identified the range of 'r' values that cause 'A' to increase by 10% above its
minimum value.
The analysis and code implementation provided a comprehensive solution to
the conical paper cup problem, allowing us to determine the optimal
dimensions for minimizing the paper surface area while meeting the specified
volume constraint.
Question 10
Part a)

function x = computeDistance(W, k1, k2, d)


if W <= 0
x = 0; % No weight, no displacement
elseif W <= d * k1
x = W / k1; % Weight within the capacity of k1
else
x = (W+2*k2*d)/(k1+2*k2); % Weight exceeding k1 capacity
end
end

Part b)

% Define constants
k1 = 1e4;
k2 = 1.5e4;
d = 0.1;

% Create an array of W values


W_values = 0:100:3000;

% Initialize an array to store corresponding x values


x_values = zeros(size(W_values));

% Calculate x for each W


for i = 1:length(W_values)
x_values(i) = computeDistance(W_values(i), k1, k2, d);
end

% Create the plot


figure;
plot(W_values, x_values, 'LineWidth', 2);
title('Distance vs. Weight');
xlabel('Weight (N)');
ylabel('Distance (m)');
grid on;

% Label the plot


legend(['k1 = ' num2str(k1) ', k2 = ' num2str(k2) ', d = ' num2str(d)],
'Location', 'NorthWest');

Conclusion
In conclusion, the MATLAB function successfully computes the distance 'x' as a
function of weight 'W' and the given spring constants and displacement 'd',
providing a clear relationship between them. The resulting plot illustrates how
'x' varies with 'W' for the specified constants, offering valuable insights into the
behavior of the mass-spring system.

You might also like