0% found this document useful (0 votes)
17 views11 pages

Tran Anh Duy - EEEEIU23007-HW2

Uploaded by

trananhduy020105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Tran Anh Duy - EEEEIU23007-HW2

Uploaded by

trananhduy020105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ex 1:

% Define the function f(x) using indicator

functions function y = f_indicator(x)

y = -x.^2 .* (x <= 0) + 3 .* (x > 0 C x < 3) + x .* (x >= 3);

endfunction

% Define the function f(x) using if

statements function y = f_if(x)

y =

zeros(size(x));

for i =

1:length(x) if

x(i) <= 0

y(i) = -

x(i)^2;

elseif x(i)

< 3 y(i) =

3;

else

y(i) =

x(i); endif

endfor

endfunctio

% Define the function x(t) using indicator

functions function y = x_indicator(t)

y = 1 .* (t >= -2 C t < -1) + 3 .* (t >= -1 C t < 1) + 4 .* (t >= 1 C t < 3) + 2 .* (t


>= 3 C t <= 4);

endfunction

% Define the function x(t) using if statements

function y = x_if(t)

y=
zeros(size(t));

for i =

1:length(t)
if t(i) >= -2 CC t(i) <

-1 y(i) = 1;

elseif t(i) >= -1 CC t(i)

< 1 y(i) = 3;

elseif t(i) >= 1 CC

t(i) < 3 y(i) = 4;

elseif t(i) >= 3 CC t(i)

<= 4 y(i) = 2;

endif

endfor

endfunctio

% Generate x values

for f(x) x = linspace(-3,

5, 100);

% Generate t values

for x(t) t = linspace(-4,

5, 100);

% Calculate y values for f(x) using both

methods y_indicator = f_indicator(x);

y_if = f_if(x);

% Calculate y values for x(t) using both

methods x_indicator_vals = x_indicator(t);

x_if_vals = x_if(t);

% Plot

f(x)

figure(1)

;
plot(x, y_indicator, 'b-', 'LineWidth', 2);
hold on;

plot(x, y_if, 'r--', 'LineWidth', 2);

xlabel('x');

ylabel('f(x)');

title('Plot of f(x)');

legend('Indicator Function', 'If

Statements'); grid on;

% Plot

x(t)

figure(2)

plot(t, x_indicator_vals, 'b-', 'LineWidth',

2); hold on;

plot(t, x_if_vals, 'r--',

'LineWidth', 2); xlabel('t');

ylabel('x(t)');

title('Plot of x(t)');

legend('Indicator Function', 'If

Statements'); grid on;

Exercise 2:

clear all; close all; clc;

% Define f1(x, n) using a for

loop function result =

f1_for(x, n) result = 0;

for k = 1:n

result = result +

x^k / k; endfor

endfunction

% Define f1(x, n) using a while loop


function result =

f1_while(x, n) result = 0;

k = 1;

while k <= n

result = result +

x^k / k; k = k + 1;

endwhile

endfunction

% Define f2(x, n) using a for

loop function result =

f2_for(x, n) result = 0;

for k = 0:n-1

result = result +

x^k; endfor

endfunction

% Define f2(x, n) using a

while loop function result =

f2_while(x, n) result = 0;

k = 0;

while k <= n-1

result = result +

x^k; k = k + 1;

endwhile

endfunction

% Generate n

values n = 1:20;
% Calculate f1(0.5, n) and f2(0.5, n) using both

methods f1_for_vals = arrayfun(@(n) f1_for(0.5, n),

n); f1_while_vals = arrayfun(@(n) f1_while(0.5, n),

n); f2_for_vals = arrayfun(@(n) f2_for(0.5, n), n);

f2_while_vals = arrayfun(@(n) f2_while(0.5, n), n);

% Plot the results in one figure with two

subplots figure;

% Subplot 1: f1(0.5,

n) subplot(2, 1, 1);

plot(n, f1_for_vals, 'b-', 'LineWidth',

2); hold on;

plot(n, f1_while_vals, 'r--', 'LineWidth',

2); xlabel('n');

ylabel('f_1(0.5, n)');

title('Plot of f_1(0.5, n)');

legend('For Loop', 'While

Loop'); grid on;

% Subplot 2: f2(0.5,

n) subplot(2, 1, 2);

plot(n, f2_for_vals, 'b-', 'LineWidth',

2); hold on;

plot(n, f2_while_vals, 'r--', 'LineWidth',

2); xlabel('n');

ylabel('f_2(0.5, n)');

title('Plot of f_2(0.5, n)');


legend('For Loop', 'While

Loop'); grid on;

% Bonus: Prove the derivative

relationship syms x n k;

f1(x, n) = symsum(x^k/k, k, 1, n);

f2(x, n) = symsum(x^k, k, 0,

n-1); df1 = diff(f1, x);

disp('Derivative of f1(x, n):');

disp(df1);

disp('f2(x,

n):');

disp(f2);

% Compare the results

numerically x_val = 0.5;

n_val = 10; % Choose a value for n

df1_num = double(subs(df1, {x, n}, {x_val,

n_val})); f2_num = double(subs(f2, {x, n},

{x_val, n_val}));

disp(['Numerical value of df1(0.5, 10): ', num2str(df1_num)]);

disp(['Numerical value of f2(0.5, 10): ', num2str(f2_num)]);

Exercise 3:

clear all; close all; clc;

% 1. Generate

matrix A A =

randi([0, 20], 5, 5);

% 2. Generate

vector b b = -10 +

20 * rand(1, 5);
% 3. Insert b below the second

row of A A = [A(1:2, :); b; A(3:end,

:)];

% 4. Delete the fourth column

of A A(:, 4) = [];

% 5. Find the minimum and maximum value with their

index [min_val, min_ind] = min(A(:));

[row_min, col_min] = ind2sub(size(A),

min_ind); [max_val, max_ind] = max(A(:));

[row_max, col_max] = ind2sub(size(A), max_ind);

% Display min and max values with their

indices disp('Minimum value:'); disp(min_val);

disp('Minimum value index:'); disp([row_min,

col_min]); disp('Maximum value:'); disp(max_val);

disp('Maximum value index:'); disp([row_max, col_max]);

% 6. Find all elements larger

than 5 larger_than_5 = A(A >

5);

% Display elements larger than 5

disp('Elements larger than 5:');

disp(larger_than_5); Exercise 4:

function x =

recursive_sequence(n) if n

<= 3

x=

n;

else

x = recursive_sequence(n-1) + 2*recursive_sequence(n-2) +
3*recursive_sequence(n-3);
endif

endfunction

% Generate n values

n_values = 1:20;

% Calculate x_n values

x_values = arrayfun(@recursive_sequence, n_values);

% Plot x_n versus n

plot(n_values, x_values, 'o-', 'MarkerSize', 5,

'LineWidth', 2); xlabel('n');

ylabel('x_n');

title('Recursive Sequence

x_n'); grid on;

You might also like