Sol Matlab
Sol Matlab
Solution
1. Problem 1
% Compute A
A = (3*sqrt(3)/4) + 24 * ( (1/12) - (1/(5*2^5)) - (1/(28*2^7)) - (1/(72*2^9)) );
% Compute B
B = 3 + (1/60) * ( 8 + (2*3)/(7*8*3) * (13 + (3*5)/(10*11*3) * (18 + (4*7)/(13*14*3)))
);
% Display results
fprintf('A = %.6f\n', A);
fprintf('B = %.6f\n', B);
2. Problem 2
WITH NO INPUT
% MATLAB script to compute x, y, and z
% Compute x
x = (2*sqrt(3))/5;
% Compute y
y = (2*pi)/exp(-x^3 / 3);
% Compute z
z = 4*x + log10(2*y);
% Display results
fprintf('x = %.6f\n', x);
fprintf('y = %.6f\n', y);
fprintf('z = %.6f\n', z);
CUSTOM INPUT
% MATLAB script to compute x, y, and z with user input
% Display results
fprintf('Computed values:\n');
fprintf('x = %.6f\n', x);
fprintf('y = %.6f\n', y);
fprintf('z = %.6f\n', z);
“%” indicates that a format specifier is being used, ”.6” means that 6 digits after the decimal
point will be shown. “f” stands for floating-point numbers, meaning the numbers will be
displayed in a decimal format.
3. Problem 3
%No input
A = [4, -6, 5; -6, 15, -3; 2, -7, 13];
B = [1, 4, -5; 2, 23, 7; -5, 9, 19];
% Display matrices
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
% Part (d): Compute the product of A and B (if dimensions are compatible)
if colsA == rowsB
prodAB = A * B;
disp('Product of A and B:');
disp(prodAB);
else
disp('Cannot multiply A and B: Incompatible dimensions.');
end
4. Problem 4
NO INPUT
y = 2*x^6 - 3*x^4 + 4*x^2 - 1988
solve(y == 0)
double(ans)
% Display results
fprintf('x = %.6f\n', ans);
CUSTOM INPUT
% User input y
y = input('Enter y = ');
solve(y == 0)
double(ans)
% Display results
fprintf('x = %.6f\n', ans);
5. Problem 5
NO INPUT
% Define the coefficient matrix A
A = [2 1 1;
-1 1 -1;
1 2 3];
% Display matrices
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
% Initial guess
x0 = 0;
y0 = 0;
% Initialize variables
x_old = x0;
y_old = y0;
iteration = 0;
disp('Iteration | x | y |');
disp('-----------------------------------------');
7. Problem 7
% MATLAB program to compute, display, and plot the finite Fourier series
% y(x) = sum_{n=1}^{1000} sin(nx)/n over the interval x = (0, 8pi)
% Step 3: Display the sum at specific x-values (e.g., at x = pi, 2pi, ..., 8pi)
x_points = pi:pi:8*pi; % Points to evaluate: pi, 2pi, ..., 8pi
fprintf('Values of the Fourier series sum y(x) = sum_{n=1}^{1000} sin(nx)/n:\n');
fprintf('-------------------------------------------------\n');
fprintf('x\t\t\ty(x)\n');
fprintf('-------------------------------------------------\n');
for i = 1:length(x_points)
% Find the index of the closest x-value in the array to x_points(i)
[~, idx] = min(abs(x - x_points(i)));
fprintf('%.4f\t\t%.4f\n', x_points(i), y(idx));
end
fprintf('-------------------------------------------------\n');
n = 0;
sum_divided_by_3 = 0;
% The loop continues one step after the sum exceeds or equals 1000,
% so the maximum value of n is the previous value.
max_n = n - 1;
10. Problem 10