MATLAB solution code for the Scientific Check assignment. Please read the readme.pdf file for easier navigation through the codebase files.
Self-Assesment Exercises: Basic programming
Questions:
- Write functions that take a matrix M of size N × N as an input and calculate the following quantities: a) The trace of the matrix. b) The sum, mean and maximum of the entries. c) The sum, mean and maximum of the absolute value of the entries. d) A vector of size N × 1 containing the mean of each row. e) A vector of size N × 1 containing the mean of each column. f) A matrix of size N × N containing the result of multiplying M with its transpose.
- Write a function that takes a matrix M of size N × N and a real number a as inputs and returns a matrix A of size N × N where the entry A(i, j) is 1 if M(i, j) ≥ a and −1 otherwise.
- Test all the functions using the matrix M of size 20×20 with entries given as M(i, j) = 1/2 (i − j) where i, j = 0, ..., 19
- Write a program which takes a vector of size N × 1 whose entries are real numbers and sorts it in ascending order.
- Write a program which computes prime numbers up to a given maximal number using the iterative algorithm called sieve of Eratosthenes. Note: - Write the corresponding code in any language without using built-in functions that can directly give the wanted results.
Answers:
Writing the corresponding codes in MATLAB without using built-in functions and comparing the non-built-in function results with built-in function results.
- Writing a MATLAB function code to compute solution for all sub-questions in question 1. Function code file is attached below with the name called “Assesment_function_code_1.m”
- Writing a MATLAB function code to compute solution for question 2. Function code file is attached below with the name called “Assesment_function_code_2.m”
- To test the above written function codes writing a script code which also includes comparison of non-built-in function results with built-in function results. Script file is attached below with the name called “Test_code.m”
- Writing a code which can sort any kind of real number array into ascending order. Code file attached below with name called “Real_Num_Ascending_order.m”
- Writing a code which can computes prime numbers up to a given maximal number using the iterative algorithm called sieve of Eratosthenes. Code file attached below with name called “Prime_Num_sieve_Eratosthenes.m”