Untitled
unknown
plain_text
2 months ago
3.0 kB
8
Indexable
%% 1. Create a vector of the even whole numbers between 31 and 75. evens = 32:2:74; %% 2. Let x = [2, 5, 16] x = [2, 5, 16]; % a. Add 16 to each element of x x_a = x + 16; % b. Add 3 to just odd index elements x_b = x; x_b(1:2:end) = x_b(1:2:end) + 3; % c. Compute the square roots and squares of each element sqrt_x = sqrt(x); square_x = x.^2; %% 3. Let x = [3, 2, 6, 8] and y = [4; 1; 3; 5] x = [3, 2, 6, 8]; y = [4; 1; 3; 5]; % a. Add the sum of x to y y_a = y + sum(x); % b. Raise each element of x to the power of corresponding y x_b = x.^y'; % c. Divide each element of y by corresponding element of x y_c = y ./ x'; % d. Multiply each element of x by corresponding element of y z = x .* y'; % e. Sum elements in 'z' and assign to 'w' w = sum(z); % f. Compute x*y - w result = x * y - w; %% 4. Create vectors % a. 2,4,6,8,... vec_a = 2:2:20; % b. 10,8,6,4,2,0,-2,-4 vec_b = 10:-2:-4; % c. 1,1/2,1/3,1/4,1/5,... vec_c = 1 ./ (1:10); %% 5. Create vector and sum first 100 elements n = 1:100; x_n = (-1).^(n+1) ./ (2*n - 1); sum_xn = sum(x_n); %% 6. Plot x, x^2, e^x, e^{x^2} for 0<x<4 x = linspace(0, 4, 100); figure; plot(x, x, 'r', x, x.^2, 'g', x, exp(x), 'b', x, exp(x.^2), 'm'); legend('x', 'x^2', 'e^x', 'e^{x^2}'); xlabel('x'); ylabel('f(x)'); title('Function Plots'); %% 7. Plot f(x) = sin(1/x) for 0.01<x<0.1 x = linspace(0.01, 0.1, 100); y = sin(1 ./ x); figure; plot(x, y); xlabel('x'); ylabel('sin(1/x)'); title('Plot of f(x) = sin(1/x)'); %% 8. Given A = [2 4 1; 6 7 2; 3 5 9] A = [2 4 1; 6 7 2; 3 5 9]; % a. Assign first row of A to x1 x1 = A(1, :); % b. Assign last two rows to y y = A(2:3, :); % c. Compute sum over columns and rows col_sum = sum(A); row_sum = sum(A, 2); %% 9. Given A and B A = [24 1; 6 7 2; 3 5 9]; B = [9 1 2 3 4 5 6 7]; % a. Assign even-numbered columns of A to B B_even = A(:, 2:2:end); % b. Assign odd-numbered rows to C C = A(1:2:end, :); % c. Convert A into a 4x3 matrix (reshaping is not possible due to dimensions) % d. Compute reciprocal of each element of A reciprocal_A = 1 ./ A; % e. Compute square root of each element of A sqrt_A = sqrt(A); %% 10. Create a vector with integers from 21 to 35 vec_10 = 21:35; %% 11. Create a vector of multiples of 7 from 21 to 105 vec_11 = 21:7:105; %% 12. Create a vector with 8 equally spaced components from 6 to 9 vec_12 = linspace(6, 9, 8); %% 13. Create a vector of square roots from 1 to 10 vec_13 = sqrt(1:10); %% 14. Graph y = x^2 for -2 < x < 3 x = linspace(-2, 3, 100); y = x.^2; figure; plot(x, y); xlabel('x'); ylabel('y = x^2'); title('Graph of y = x^2'); %% 15. Plot y = e^(-x) and y = arctan(x) for -3 < x < 3 x = linspace(-3, 3, 100); y1 = exp(-x); y2 = atan(x); figure; plot(x, y1, 'r', x, y2, 'b'); legend('e^{-x}', 'arctan(x)'); xlabel('x'); ylabel('y'); title('Plot of e^{-x} and arctan(x)'); %% 16. Solve for Fahrenheit temperature given Celsius formula syms F; C = solve(5/9 * (F - 32) == -2, F); disp(double(C));
Editor is loading...
Leave a Comment