BT MatLab
BT MatLab
clear; clc;
x = 1;
y = input ('y = ');
number=x:0.5:y;
Bài 2:
clear; clc;
number = 0:5:45;
for k = 1:1:length(number)
val(k) = number(k);
fprintf('%1.0f\n', val);
k = k + 1;
end
fprintf('\n');
Bài 3:
clear; clc;
number = 45:-5:0;
for k = 1:1:length(number)
val(k) = number(k);
fprintf('%1.0f\n', val);
k = k + 1;
end
fprintf('\n');
Bài 4:
clear; clc;
Bài 5:
clear; clc;
startNumber = input('Enter number 1: ');
stopNumber = input('Enter number 2: ');
if stopNumber < startNumber
disp('The start number must be less than the end number.')
else
for num = startNumber : stopNumber
fprintf('%d ', num);
end
fprintf('\n');
end
Bài 6:
clear, clc, close all;
% create array t of independent variable values
% evaluate f(t) for all the values in t
% plot t versus f(t)
t = 0:0.01:3;
ft = 4 - t.^2 .* exp(-3*t);
plot(t, ft);
xlabel('Thoi gian(s)');
ylabel('f(t)');
title('Do thi ham so f(t)=4-t^2*e^(-3t)');
grid("on");
legend('f(t)');
Bài 7:
clear, clc, close all;
% Allocate space for the time and function arrays
% Assuming a time interval of 0.05 seconds
numberValues = 61;
t = zeros(1, numberValues);
f = zeros(1, numberValues);
% Create t and f values
for i = 1:numberValues
% Create next time value
t(i) = (i - 1) * 0.05;
% Evaluate f(t) for the time value and store it in f array
f(i) = 4 - (t(i)^2) * exp(-3 * t(i));
end
% Plot t versus f(t)
plot(t, f);
xlabel('Thoi gian(s)');
ylabel('f(t)');
title('Plot of f(t)=4-(t^2)*e(-3t)');
grid on;
Bài 8:
clear; clc;
% read in number of terms N
N = input("Enter the number of terms: ");
% create an array of the term numbers
term_number = 0:N-1;
% evaluate term function for each element in term number array
term = (-1).^term_number .* (1./(2.^(term_number)));
% sum the array of terms
result = sum(term);
disp("The sum of the series is: " + num2str(result));
Bài 9:
clear; clc;
% read in number of terms N
N = input('Enter the number of terms N: ');
% allocate space for result and set initial value of result
result = 0;
% iterate over the term numbers
for termNumber = 1:N
% evaluate term function for term number
term = (-1)^(termNumber-1) / 2^(termNumber-1);
% add new term to previous result and update result
result = result + term;
end
% display the result
disp(['The sum of the series for N terms is: ' num2str(result)]);
Bài 11:
clear; clc;
% input the array
array = input('Enter the array: ');
% initialize minimum and maximum values and their locations
minValue = array(1);
maxValue = array(1);
minIndex = 1;
maxIndex = 1;
% iterate over the elements of the array
for i = 2:length(array)
% check if current element is smaller than the minimum value
if array(i) < minValue
minValue = array(i);
minIndex = i;
end
% check if current element is larger than the maximum value
if array(i) > maxValue
maxValue = array(i);
maxIndex = i;
end
end
% display the minimum and maximum values and their locations
disp(['Minimum value: ' num2str(minValue) ', Location: ' num2str(minIndex)]);
disp(['Maximum value: ' num2str(maxValue) ', Location: ' num2str(maxIndex)]);
Bài 12:
clear; clc;
% Generate noisy data
noisyData = 10.0 + 2.0*randn(1, 100);
% Initialize smoothed data array
smoothedData = zeros(1, length(noisyData));
% Smooth the data using a two-point average
for k = 1:length(noisyData)
if k == 1
smoothedData(k) = noisyData(k);
else
smoothedData(k) = (noisyData(k) + noisyData(k-1))/2;
end
end
% Plot the original and smoothed data
plot(noisyData, 'b-', 'LineWidth', 1.5);
hold on;
plot(smoothedData, 'r-', 'LineWidth', 1.5);
legend('Noisy Data', 'Smoothed Data');
xlabel('Index');
ylabel('Value');