Saturday 16 June 2012

Some Simple Matlab Exercises - Code Samples I

Some Simple Matlab Exercises - Code Samples I

You may find some Matlab Commands here - http://jayitsecurity.blogspot.com/2013/01/matlab-commands-operators-and-special.html

Write a pseudocode and matlab program which receive a positive integer value as input and then output the following message “Even Number” or “Odd Number”.

Example:

Input:           Enter a number: 7
Output:        Odd number

Input:           Enter a number: 8
Output:        Even number

Answer:

Pseudocode

Start
Read input Num
                x = Modulus (Num, 2)
                If x equals 0
                                then display Num is an even number
                Else
                                display Num is an odd number
End


Matlab program
Num = input('Enter a positive integer number: ')

x = mod(Num,2);

if x == 0
    sprintf('%d is an even number', Num)
   
else
    sprintf('%d is an odd number', Num)
   
end

Sample outputs

Enter a positive integer number: 5
ans =
5 is an odd number

Enter a positive integer number: 8
ans =
8 is an even number

Write the expression of matlab language which is equivalent with the following mathematical expressions:

a.     b– 4ac
b.     (a + b) (c + d) (e + f)

Answer:

a.     b*b – 4*a*c
b.     (a + b)*(c + d)*(e + f)


A chemist uses the decision table below to determine whether a solution is very acidic, acidic, neutral, alkaline, or very alkaline. Write a program that will read in the value of pH for a solution and display its type.

Table 1

pH range
Liquid types
>12
8 – 12
7
3 – 6
<= 2
very alkaline
alkaline
neutral
acidic
very acidic

Answer:

clear all, clc

pH = input('Enter pH value: ');

if pH>12
            sprintf('Liquid Type: Very Alkaline')

elseif pH>=8 & pH<=12
            sprintf('Liquid Type: Alkaline')

elseif pH==7
            sprintf('Liquid Type: Neutral')

elseif pH>=3 & pH<=6
            sprintf('Liquid Type: Acidic')

else
            sprintf('Liquid Type: Very Acidic')

end

Sample outputs

Enter pH value: 1
ans =
Liquid Type: Very Acidic

Enter pH value: 11
ans =
Liquid Type: Alkaline

Enter pH value: 7
ans =
Liquid Type: Neutral


Write a program which accepts input time in second and then output it in hour, minute and second.


Example:

Enter time in second: 3770
3770 seconds = 1 hour 2 minutes 50 seconds.

Answer:

Matlab program

clear all, clc;

InputSeconds = input('Enter time in seconds: ');

Seconds = mod(InputSeconds,60);

Bal_Seconds = InputSeconds - Seconds;

Hours = int8(Bal_Seconds/3600);

Minutes = int8(mod(Bal_Seconds,3600)/60);

sprintf('%d second(s) = %d hour(s) %d minute(s) %d second(s)',InputSeconds,Hours,Minutes,Seconds)

Sample outputs

Enter time in seconds: 3770
ans =
3770 second(s) = 1 hour(s) 2 minute(s) 50 second(s)

Enter time in seconds: 7281
ans =
7281 second(s) = 2 hour(s) 1 minute(s) 21 second(s)

Enter time in seconds: 325
ans =
325 second(s) = 0 hour(s) 5 minute(s) 25 second(s)


Popular Posts