Introduction to programming languages
(recapitulation from the last lecture)
To write a computer program, you have to tell the computer, step by step, exactly what you want it to do. The computer then "executes" the program, following each step mechanically, to accomplish the end goal.
You have to write the algorithm using some programming language: Matlab, C, ...
Source: https://computer.howstuffworks.com/what-is-a-computer-algorithm.htm.
Basics
-
Arithmetic operations
+ add - subtract * multiply / divide ^ power
-
Variables
the way a program remembers things (values)
a variable has a name and a type (number, text, ...)a, A, x3, Deposit, Gross_interest
-
Assignement statement
sets the value stored in a variablex = 3; x3 = N - 7/2; y = y + 5^0.3; Name_1 = 'Jan'; k = k+1;
the expression on the right is evaluated and its value is stored in the variable on the left -
Input and Output operations
x = input('give a number: '); Name_1 = input('give a name: '); disp('Name_1'); % displays the name of the variable disp(Name_1); % displays the value of the variable % how to display both in the same line: disp(['Name_1 is equal to ' num2str(Name_1)]);
-
Comments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Computation of the gross interest %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % input data Deposit = input('Give the deposit:'); Rate = input('Give the interest rate:'); N_years = input('Give the number of years:'); % the computation q = 1 + Rate/100; Gross_interest = Deposit * q^ N_years - Deposit; % the output disp('The gross interest is'); disp(Gross_interest);