Introduction to Matlab. Creating Matlab Scripts. Birth Year Program. Get the user’s age as input Display the user’s birth year Use input() to get the age Use disp to display the year. Type in Your Program. Give Your Script a Name. Run Your Program. Program in Action. Search Path.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Introduction to Matlab
Sample run :
Please pick a number between 10 and 99
Now add the two digits of your number
Substract the result from your number and enter the result: 36
Now subtract 4 from your number and enter the result: 42
The number you picked was 46
% Finds out the two digit number the user is thinking of
disp('Please pick a number between 10 and 99');
disp('Now add the two digits of your number');
nineA = input('Substract the result from your number and enter the result: ');
A = nineA / 9;
nineAoneB = input(['Now subtract ' num2str(A) ...
' from your number and enter the result: ']);
B=nineAoneB - 9 * A;
disp(['The number you picked was ' num2str(10*A+B) ]);
disp(['In other words, it was ' num2str(A) num2str(B) ]);
Please enter a number 56789
Which digit do you want ? 2
Your digit is : 8
% Gets a number from user and a digit no. starting with 1
% and returns the numeric value of that digit
n=input('Please enter a number ');
k=input('Which digit do you want ? ');
tenToK = 10^k;
firstK=rem(n,tenToK);
K=floor(firstK/ (tenToK/10));
disp(['Your digit is : ' num2str(K)]);
>> str = input('Enter a string: >', 's');
Enter a string: >so were the days of fall
>> disp(['The length of your string was : ' num2str(length(str)) ' characters ...']);
The length of your string was : 24 characters ...
>> str = input('Enter a string: >');
Enter a string: >having said all that,
??? having said all that,
Error: Missing operator, comma, or semicolon.
>> s = 'abcde';
>> s(1)
ans =a
>> s(2)
ans =b
strn=input('Please enter a number ', 's');
k=input('Which digit do you want ? ');
location = length(strn) - k + 1;
disp(['Your digit is : ' strn(location)]);
Multiply your shoe size with 5 and press enter ...
Now add 50, press enter when ready ...
Now multiply the result with 20 an then add 1003 ...
Subtract your birth year from the final result and enter the value >5030
Your shoe size is : 50
Your age is : 30
input('Multiply your shoe size with 5 and press enter ...');
input('Now add 50, press enter when ready ...');
input('Now multiply the result with 20 an then add 1003 ...');
sizeAge = input('Subtract your birth year from the final result and enter the value >');
size = floor(sizeAge/100);
age=rem(sizeAge, 100);
disp(['Your shoe size is : ' num2str(size) ]);
disp(['Your age is : ' num2str(age) ]);
1.Enter kitchen
2.Get Glass
3.Get milk from fridge
4.Fill glass with milk
5.Drink it!
1.Enter kitchen
1.1walk to the kitchen door
1.2go through door
1.3walk into kitchen
2.Get Glass
3.Get milk from fridge
3.1open fridge
3.2get milk
3.3close fridge
4.Fill glass with milk
5.Drink it!
<---door may be closed !
What if door is already open !
1.Enter kitchen
1.1walk to the kitchen door
1.2open door and go through it
1.3walk into kitchen
2.Get Glass
3.Get milk from fridge
3.1open fridge
3.2get milk
3.3close fridge
4.Fill glass with milk
5.Drink it!
1.Enter kitchen
1.1walk to the kitchen door
1.2if door is closed then
1.2.Topen it
1.3walk into kitchen
2.Get Glass
3.Get milk from fridge
3.1open fridge
3.2get milk
3.3close fridge
4.Fill glass with milk
4.1while glass not full
4.1.1pour some milk into it
5.Drink it!
condition
evaluated
true
false
statements
Pseudo-code :
if (condition)
statement1 statement2
…
Matlab:
if (condition)
statement1
statement2
…
end
condition
evaluated
statementT
statementF
Matlab:
if (condition)
statementT1
statementT2
…
else
statementF1
statementF2
…
end
true
false
check patient’s temperature
Is temp. < 36?
Yes
No
Is temp. > 39?
Yes
No
Put on a sweater
Take an aspirin
Go home!
Matlab Way:
if (temp < 36)
put on a sweater
else
if (temp > 39)
take an aspirin
else
go home
end
end
Pseudo-code:
if (temp < 36)
put on a sweater
else
if (temp > 39)
take an aspirin
else
go home
Matlab Way:
if (temp < 36)
put on a sweater
elseif (temp > 39)
take an aspirin
else
go home
end
Pseudo-code:
if (temp < 36)
put on a sweater
else if (temp > 39)
take an aspirin
else
go home
if value negative
true
false
display value
negate value
get value from user
value=input('Enter a number : >');
if (value < 0)
value = -value;
end
disp(['The absolute value of your number is ' … num2str(value) ]);
get value from user
if value is real
if value is negative
absValue is negative of value
else
absValue = value
else if real part of value is zero
absValue = value
if (absValue < 0)
absValue = -absValue
else
absValue is square root of sum of squares of real and imaginary parts of value
Display absValue as the result
value=input('Enter a number : >');
absValue=0;
if (isreal(value)) % or if imag(value) == 0
if (value < 0)
absValue = -value;
else
absValue = value;
end
elseif (real(value) == 0)
absValue=imag(value);
if (absValue < 0)
absValue = -absValue;
end
else
absValue = sqrt(real(value)^2 + imag(value)^2);
end
disp(['The absolute value of your number is ' num2str(absValue) ]);
Enter a number : >3
The absolute value of your number is 3
>> absolute_complex
Enter a number : >-3
The absolute value of your number is 3
>> absolute_complex
Enter a number : >3i
The absolute value of your number is 3
>> absolute_complex
Enter a number : >-3i
The absolute value of your number is 3
>> absolute_complex
Enter a number : >3+4i
The absolute value of your number is 5
>> absolute_complex
Enter a number : >3-4i
The absolute value of your number is 5
>> absolute_complex
Enter a number : >-3-4i
The absolute value of your number is 5
Give an algorithm for deciding what to play. Rules are as follows:
If raining
if temp > 15
go indoor swimming (raining & temp > 15)
else if no lightining
play soccer (raining & temp < 15 & no lightining)
else
play ping pong (raning & temp < 15 & lightining)
else if cloudy
go running
else if temp < 35
play golf
else
stay at home
if (123.45)
if (0)
if (true)
absValue=0;
if (imag(value) == 0 || real(value) == 0)
absValue=imag(value) + real(value);
if (absValue < 0)
absValue = -absValue;
end
else
absValue = sqrt(real(value)^2 + imag(value)^2);
end
absValue=0;
if (imag(value) ~= 0 && real(value) ~= 0)
absValue = sqrt(real(value)^2 + imag(value)^2);
else
if (imag(value) == 0)
absValue = real(value);
else % do not need to ask real(value) == 0
absValue = imag(value);
end
if (absValue < 0)
absValue = -absValue;
end
end
Individual Questions:
if (temp < 36)
put on a sweater
end
if (temp > 39)
take an aspirin
end
if (temp <= 39 && temp >= 36)
go home
end
Questions chained :
if (temp < 36)
put on a sweater
elseif (temp > 39)
take an aspirin
else
go home
end