Beruflich Dokumente
Kultur Dokumente
% By Wenbin Wang
% You may need more functions in your homework and lab reports. You can use
% these two tools to help you understand your functions better.
%% MATLAB as a calculator
3 + 5;
5 * 3;
18 ./ 5;
1e-3 % Scientific notation
sin(10) % Sin, in radians
3*sqrt(9) % Square root
2.^2 % Squared
exp(3) % Exponential
%% format
format long % Scaled fixed point format with 15 digits
5./2
pi
format short % Scaled fixed point format with 5 digits
5./2
pi
% Notice that Matlab echos the results, which are saved in a variable name
"ans"
%% variables
a = 10; % You can name your own variables
b = 5;
c = a + b
% Reserved keywords
a = 1 + 2i % Complex number
i % Imaginary unit
j % Same to i
pi
% Manipulate a matrix
a(3) % The third element of a
a(:) % All emelents in a
a(:,:) % All elements in a
a(:, 1:2) % Take the 1st and 2nd columns
a(1:2, 1:2) % The 1st and 2nd columns and 1st and 2nd rows
a(2, end) % Last element in the second row
a(1:2:3, end-1) % The second to the last elements in the 1st & 3rd row
%% Plot
figure; % Create figure window
x = linspace(1, 5, 100); % Give a linspaced vector.
% An alternative way
x_test = 1:0.05:5;
% try x = 1:5;
y = sin(x);
plot(x, y); % Plot y(x)
xlabel('x'); % Label x
ylabel('sin'); % Label y
% xlabel('x_2') % Subscripts
% xlabel('\theta') % Greek letters
title ('Sine function'); % give a title
legend('sin'); % Legend
grid on; % Turn on grid
subplot(1, 2, 2)
plot(x, y2, 'b');
title ('Cosine function');
xlabel('\theta (rads)');
ylabel('Cosine of \theta');