Need help? We are here


Question 1

Answer the questions below…

Login to MATLAB

Try the following: cut and paste in MATLAB a few lines a ta at time:

% chars hold single characters

c1 = ‘A’

%————————————————————-

% class displays the data type

class(c1)

%————————————————————-

% You can store strings in single quotes

s1 = ‘A string’

class(s1)

%————————————————————-

% Booleans map true to 1 and false to 0 

7 > 3

b1 = true

%————————————————————-

% Show max & min values

intmin(‘int8’)

intmax(‘int8’)

% Largest double

realmax

% Largest int

realmax(‘single’)

%————————————————————-

% You can continue expressions using …

% Suppress output with a ;

v1 = 1 + 2 + 3 + 4…

+ 5;

% Everything defaults to double precision

v2 = 7

class(v2)

%————————————————————-

% Cast to int8

% The term casting refers to the change of the type

% without transformation of its content

v3 = int8(v2)

class(v3)

% Convert char to double

v4 = double(‘A’)

% Convert to char

v5 = char(64)

%————————————————————-

% sprintf formats a string

% %d : Integers, %f : Floats, %e : exponential notation

% %c : Characters, %s : Strings

fprintf(‘5 + 4 = %dn’, 5 + 4)

fprintf(‘5 – 4 = %dn’, 5 – 4)

fprintf(‘5 * 4 = %dn’, 5 * 4)

% Define you want only 2 decimals

fprintf(‘5 / 4 = %0.2fn’, 5 / 4)

% Exponentiation

fprintf(‘5^4 = %dn’, 5^4)

% Modulus (Escape % by doubling)

fprintf(‘5 %% 4 = %dn’, mod(5,4))

% Generate a random value between 10 & 20

randi([10,20])

% Precision is accurate to 15 digits by default

bF = 1.1111111111111111

bF2 = bF + 0.1111111111111111

fprintf(“bF2 = %0.16fn”, bF2)

%————————————————————-

% help elfun shows a list

fprintf(‘abs(-1) = %dn’, abs(-1))

fprintf(‘floor(2.45) = %dn’, floor(2.45))

fprintf(‘ceil(2.45) = %dn’, ceil(2.45))

fprintf(’round(2.45) = %dn’, round(2.45))

fprintf(‘exp(1) = %fn’, exp(1)) % e^x

fprintf(‘log(100) = %fn’, log(100))

fprintf(‘log10(100) = %fn’, log10(100))

fprintf(‘log2(100) = %fn’, log2(100))

fprintf(‘sqrt(100) = %fn’, sqrt(100))

fprintf(’90 Deg to Radians = %fn’, deg2rad(90))

%————————————————————-

% Relational Operators : >, <, >=, <=, == and ~=

% Logical Operators : ||, &&, ~ (Not)

Try writing a few conditional statements.

Question 2

For MATLAB – Find an algorithm to perform the Bisection method and execute the following.

  1. Square root of 7. Guess a is 2, guess b is 3. 10 iterations.
  2. Square root of 11. guess A is 3, guess B is 4. 10 iterations.
  3. Square root of 11. guess A is 2, guess B is 5. 10 iterations.
  4. From question a and b – which is more accurate?
  5. Repeat c with 30 iterations. Write your observation(s) of the result.

** This is a MATLAB assignment