this is our second lecture session of the course we're going to discuss built-in functions basically and mathematical statements in matlab um following our discussion you will have an assignment that would help you practice what you learn in during our lecture we're going to have an example that is very much uh closely related to how you solve your assignment today i've also posted a sample problem number one and also quiz questions uh number one that you could take a look and test your understanding of the topics of the course we're going to begin by looking at basic mathematical operations and then expanding that to mathematical statements containing functions mathematical functions and how we deal with those let me begin by sharing my screen so mata statements and built-in functions uh our focus of our discussion today uh going to begin by looking at basic mathematical statements that contain a scalars now by scalars i mean variables that contain only a single number single value so it's not an array of you know numbers but only one row and one column array which only scaler and only contains a single number so for these types of statements we're going to make use of standard mathematical operators we have the assignment operator with the equal sign you know in mathematics and then we have other operators for addition subtraction multiplication division and power so plus minus star forward the slash and carrot are used basically for those operations as listed here so the assignment operator is something that we need to explore in more detail today first of all assignment operator has a different meaning than that you know in mathematics equal sign so the equal sign here is referred to assignment operator because what it does it waits for the right side of that operator to be executed all operations to be completed and the result of those operations would be assigned to the variable listed on the left side of that assignment operator i'll give you examples so you have now x assigned 5 so 5 would be processed there is no processing here really no addition subtraction or anything so that resultant would be a stored or assigned to x and a stored in the memory reference by x so then when you write another statement after that like y assigned x to the power of 2 then the value of x would be taken to the power of 2 so 5 to the power of 2 is 25 and now since there is no other operations to do 25 is assigned to y so then y contains really the value of 25 you could also write something like that with assignment operator but you cannot write that with the equal sign in mathematics so from mathematics point of view this statement y equals y plus 1 is really incorrect because if you cancel y from both sides you're saying zero equals one and that is a meaningless statement in mathematics however in programming that equal sign which is referred to as assignment operator is a meaningful statement so assignment operates as a say processes the right side so now right side is adding to y one so y was 25 you add 1 to it is 26 and now 26 assigned to y again which means you're kind of overriding its previous value and changing it from 25 now to 26. in a sense you had incremented the value of y by one unit any questions folks okay if you have a statement that you have many of these operators all working on that statement then you may have to consider the precedence of the operations and understand how that works because when you're writing these statements depending on how you arrange it it could give you completely different outcomes that may not be representative of the actual statement that you have at hand so the presence of the operations is that in matlab all operations within the parentheses from the innermost parenthesis coming out then is executed first the next thing that is executed is the power function from left to right and then multiplication and division whichever you face from left to right and then addition and subtraction from left to right again so these are the precedence of the operations so we're knowing those let's go and take a look at an example here let's say you've given this mathematical expression and you asked to write a math lab statement that represents this mathematical function so we want to know which one is correct is this a correct statement please study that and tell me whether that is a correct representation of that mathematical function in matlab no all right what's wrong with that since there's no indication it's just going to put x to the third and then it's going to multiply 2 by x and then it's going to divide negative 1 by x x and be done done with it it without without indicating that the whole thing on the left of of the slash is supposed to be divided by x x times x minus four okay so that's one reason there are many reasons that this statement doesn't work first of all the numerator has to be divided by denominator here and the numerator is not defined in this statement we the way it's written it says that there is a fraction of one over x then multiplied by x minus four we don't have that fraction here so the entire numerator has to be divided by denominator so then we would have to make first of all that correction so would this be then the correct representation part b i don't think it's correct i think it works all right anybody else i don't think it works could you speak up please i don't think it works what is wrong with that it's because uh in in the middle it's x divided by x minus four okay that's a good point you brought up this statement is absolutely correct and i'll explain why when you look at the actual function itself here so numerator is divided by x and then again divided by x minus 4. isn't that the case so this division has to take place twice the numerator has to be divided by x and then again divided by x minus 4. so that division by x and then the result divided by x minus 4 is acceptable however you could rewrite this in a different format like this one here where you group your denominator also inside parentheses as you see here the same way that you have grouped your numerator you group your denominator so meaning that you prepare the denominator first before you have it divided by so then uh just putting a pair of parentheses around the numer rounded denominator it defines the denominator really well and therefore this would be also acceptable b and c are acceptable but also pay attention to the precedence of operations when you look at these here these are statements you know that there are multiplications and divisions and so on and so forth and when you're looking at how these operations are done remember that first of all in this situation power takes precedence first in this case let's say let's focus on part c we have pairs of parentheses so pairs of parentheses means that inside each pair of parentheses would be executed first from left to right so this numerator that has a pair of parentheses would be executed completely first and then proceeding to the right side there is an innermost parenthesis which is this one x minus four so that would be executed and then it the result of that multiplied by x that parenthesis then would be executed so now the left parenthesis already is executed and the right outer parenthesis is also figured out and then you have a division where it takes the result of the left side and divides it by the right side and then whatever is the answer here would be assigned to x for us now an alternate solution for this would be just to simply define the uh use more statement rather than just one statement that we have in this case just use more a statement define the numerator give it a name and find that give a name to denominator and once those two are obtained then divide the numerator variable by the denominator variable that will give you y so we do it this way here as is explained we define the numerator as top so we write x cubed plus two times x minus one okay and then we define the bottom uh the nu the denominator as x multiplied by x minus four and then we say y is nothing but top over button okay any questions about how statements are written another example so you are given this matlab statement that you see y is equal to x multiplied by 1 minus x to the power of 3.6 divided by 3 plus x squared plus 1 power 1 half so which one of these are the original mathematical function that is represented by that matlab statement the top one the top one b b also b uh yeah anybody else in fact they all are acceptable because they all represent the same statement so let's take the first one in the matlab statement we have x multiplied by a pair of parentheses in which one is subtracted from x to the power of 3.6 over three so why is that between these two because between these two operators the power and division the power takes precedence if it didn't take precedence then we have to divide 3.6 by 3 right we divide 3.6 by 3 and what do we get 1.2 and then we say that's x to the power of 1.2 which would be this case here but is that really correct based on the precedence of the operators you raise x to the power of 3.6 first so this is x to the power of 3.6 and then the division takes place so then this form here in part b is correct all right so therefore we should dismiss a all together a is not correct so when it's faced it appears that it is correct but when you look at the precedence of the operations it isn't does that make sense yes okay oh sorry go ahead now c looks like okay so let's move to part b and then we go to c okay part b that first term is uh considered properly so you're multiplying x by this parenthesis which is 1 minus x to the power of 3.6 then divided by 3. so it would be then part b but what about the second term of this statement which is x squared plus 1 taken to the power of one half so which one is this part here is the square root of x squared plus 1 as listed in part b or x squared plus 1 to the power of 0.5 which one is it or are they both acceptable i think they're both because to the half power is the same as saying square root exactly exactly so half power is 0.05 and the square root also has the same meaning so that term is acceptable in all three cases a b and c but it is just the first term that is not acceptable in part a but in part b and c it is in part c it is because they have multiplied x by 1 which is x and then multiplied by x by x to the power of 3.6 so that would result in x raising to the power of 3.6 plus 1 which is 4.6 and then it's divided by 3. so parts b and c are acceptable okay any questions folks now if you have those uh basic mathematical operations that apply to arrays rather than the scalars the situation changes slightly we're no longer able to use some of those very for some of those operators and i explain why so for addition and subtraction of arrays or statements involving arrays that have additions and subtractions we use the plus and minus as before and we also use the so-called assignment operator so that those is still our value however our multiplication division and our power operators are no longer valid and we have to replace it by these new operators they are referred to as dot operators because you put a dot before the operator itself so it's dot star for multiplication dot division for division and dot caret for the power these operators therefore all results in element by element operation of arrays involved so for example when you have plus one one array plus the other one it takes the first element of the first array adds it to first element of the second array and then it repeats this between all other elements it takes for example the second element of the first array and adds it to the second element of the second array and so on and so forth they are called element by element so if these operators work on arrays element by element then the arrays would have to be the same size as a result and therefore when you have mathematical statements you have to be careful whether the operation is applied to the same size arrays or not okay so it's important to remember that so the order of the operations in this case for arrays is also similar to the one you saw a pair of parentheses starting from the innermost power from left to right multiplication and division from left to right and addition and subtraction from left to right okay so let's say you have now two arrays x has three elements one two four and y has three elements three zero and five and we like to evaluate this a statement so the right side of the assignment operator is executed first on the right side we have only a subtraction of two arrays so element by element subtraction one minus three would be minus two two minus zero is two and four minus five is minus one therefore the value of z would be this value here sorry y is coming first so three minus one is two and the next element zero minus two is minus two and then five minus four so the third element five and four five minus four is one okay so what about this one this on the right side includes a multiplication and addition so multiplication takes place first each element of x would be multiplied by the corresponding element in y so 1 times 3 is 3 2 times zero is zero and four times five is twenty so the result of this multiplication is an array of three zero and twenty so now we're adding to that array one so when you adding to an array a and a scalar a number you would be adding that to all elements or subtracting that to all elements whatever the operator is so therefore then one would be added to all elements of this new array here that we found and that results in therefore 4 1 and 21 for variable u let's take this one x is divided by 2 and it's a dot division because x is an array so then each element of x would be taken and divided by two so one divided by two is point five two divided by two is one and four divided by two is 2. take this one now we are dividing 2 by x what would be the result of this anybody two one one half two one that's correct so two would be divided by every single element of x and therefore you get two divided by 1 is 2 and the next element 2 divided by 2 is 1 and the next element 2 divided by 4 is 0.5 okay what about this one what is the answer there each element of x is raised to power of three so one to the power of three is one two to the power of three is eight and four to the power of three is sixty-four all right so those are basic uh arithmetic really operations uh but you may also write a statement uh mathematical statements that contain functions such as sine functions exponent functions logarithmic functions or many other functions matlab is rich in functions very a lot of functions exist that we're going to be discussing in the next few sessions so for matlab for functions that we find in our mathematical statements we should use built-in matlab functions and matlab functions are grouped really in different categories we begin with common math functions that we can use in our statements and this is a listing of the most common math functions that we are going to be using the first function as listed here is abs or absolute value of x it gives you the absolute value of x so if you have x equal to minus 3 and you say abs of minus 3 the answer would be then plus 3. if you have absolute of 4 then the answer is 4 because that's already positive and therefore its absolute value is the same then you have sqrt or a square root which gives you the square root of value then you have nth root x comma n it will give you nth root of x so if you want for example to raise minus 2 to the power of 1 3 minus 2 to the power of 1 3 you write it as and root that index of the name of the function parenthesis the actual value which is minus 2 and then comma 3. so that's in a sense one nth one nth power of course you can also write it the way it's written here as minus 2 to the power of one third but remember whether you use n truth or you use this form they result in different and well it depends it it appears in different form nth root does not print uh complex numbers because the answer for this is a complex number as you see here the complex number is 0.600 minus one point zero one one i that is the complex answer complex number as nth root does not give you the complex form it gives you the actual value of that complex number which is really raising the first part which is 0.6 to the power of 2 and raising the second part which is minus 1.0911 to the power of 2 sums those up and takes the square root of that that's called the size of the complex number and that value is this value here so although they appear really differently but they represent you know more or less the same thing uh the second form is more descriptive because it gives you the real part and the imaginary part of the complex number but nth root does not so remember that when you use it because you know take caution now we have another function called sine of x it gives you the sign of a number if number part is positive it returns one if it's negative returns minus one and if it is equal to zero it returns uh zero for you next function is ram or remainder of x comma y dividing of x comma y so it computes the remainder of x divided by y and as an example it says rem of 27 comma 7 is 1. you folks understand this so divide 25 by 4. what do we get there 25 by 4. one what is division of 25 by four six with the remainder of one okay so what we are used to this is important to remember what we're used to is when we say divide 25 by four we say aha this is 6.25 we automatically assume that 25 and or 4 or both of them or you know at least one of them is a real number is a number that has a decimal part and in that case dividing a real number by another number is always going to give you a decimal answer so 6.25 would be the correct one however here we are talking about division of integers so this 25 is not 25.0 it's really 25 integer and 4 is also integer and in integer division the answer has to be an integer so remember that integer division results would be an integer so out of that 6.25 you drop out what you drop out the 0.25 and you say okay this division is 6. so but then you come across a problem here right because if you multiply 6 by 4 you don't get 25 you get only 24. so then what happened to the difference there that one is the so-called the remainder of this division and rem of x and y will give you that remainder so with this one you can only use integers and you can only get integers absolutely yes got it so what is rem of 7 and 4 folks 3 is 3. good all right so moving on exp of 8 gives you the exponent of x exponent meaning that e to the power of x i'm sorry it will give you yes exponent of x which is nothing but e to the power of x e is if you remember is the base of natural log and its value is 2.7183 approximately here so exp of 10 therefore is 10 to the power i'm sorry e to the power of 10. exp of 2 is e to the power of 2. next we have log of x which is really natural log of x and then we have log 10 of x which is log base of 10 y log 10 base 10 of x okay so it's the common log of x not the natural log now the next group of functions are trig functions trigonometric functions we have a function called degree to radian so it converts a degree to a radian so on the right side if you say degree to radian of 90 it says the answer is 1.5708 so that is the radian value of that 90 degrees if you want to do the reverse radian to degree you use rad to do number two a deg degree and that converts a radiant to a degree so on the right side if you say rad to dig of pi then you said the answer is 180 degrees we have sine cosine and tangent functions of x and x has to be radiant in these functions here pay attention this function will give you the sine cosine and tangent of an angle in radian then we have a sine which will give you the inverse of the sine or sometimes referred to as arc sine so a sine we have exactly similar to this force cosine and tangent so we have a cosine of x and a tan of x they are not listed here but they exist so they all give you the inverse of that function whether it's sine cosine or tangent the angle the reverse of the inverse of those functions will give you an angle and that angle is expressed in radians remember that and then we have the next function sine h of x that's the hyperbolic sine of x we also have co c osh for cosine hyperbolic and we also have t a n h for hyperbolic at tangent of x and x here again is expressed in radians and then if you want to get the inverse or inverse of the hyperbolic function that's a sine of h of x so find the inverse hyperbolic sine of x now in all of these you have x expressed or the answer expressed in radians but what if you have degrees you can use similar functions in degrees just put a d next to the name of the function so for example you write sine d of x and then in that case x will be a degree not a radian so and then we have also cos of d for cosine when x is degree and we also have t a n d for tangent okay and then we have the inverse of those as well uh you know same logic the next set of functions are rounding functions interesting functions here so round x will give you the nearest integer to the x value so when you say round of 8.6 the nearest integer to that is 9. if you have a round of 8.2 the nearest integer is eight so that's wrong if you put another number inside round functions so you say round x and n then x is rounded to n decimal digits x is rounded to n decimal digits so if you have a value such as 8.6436 and you say round this to three decimals then it takes three decimals and drops out the last decimal but before dropping out the last decimal it kind of rounds that last decimal if it's five or larger it adds one to the decimal before it if it's a four or a smaller it drops it out so therefore it adds now in this case one two three that would be four and then the other two decimals four and six and the actual number so now that number is rounded to three decimals okay we also have fix and this is a little bit tricky round this rounds or truncate x to the nearest integer towards zero towards zero that's important so if you get a fix of 8.6 fix up 8.6 that will give you 8 not 9 it's not round really if you say round of x it will give you 9 right but if you say fix x it gives you eight it drops the decimal part because you're going to the nearest integer towards zero that's important to remember towards zero so knowing that then what is fix of minus eight point six eight okay so minus 8.6 yes that'd be uh minus eight minus eight yes as as shown here so then floor is the next one it gives you the nearest integer towards negative infinity so then when you say give me floor of minus 0.8.6 it will give you -9 because it goes towards minus infinity and ceiling does the opposite of that it rounds x to the nearest integer towards positive infinity so then when you say give me ceiling of minus 8.6 you're moving towards positive infinity so which integer are you going to be looking at going towards positive infinity the next integer would be minus 8 and the one after that minus 7 and minus six and so on and so forth so therefore ceiling of minus eight point six is equal to eight itself okay next are discrete math functions that we will be using and we have this listing factor of x will give you the prime factors of x so factor of 12 is 2 and 2 and 3. those are the prime factors gcd or greatest common denominator will give you the greatest common denominator of x and y so when you say gcd of 10 and 15 it says it's 5. least common denominator l c m of x and y will give you the least common denominator of x and y so when you say lcm of 2 and 5 it says 10. lcm of 2 and 10 is also 10. okay rats of x so this represents x in a fraction four so in a kind of rational form we may want to call it rats so when you say give me rats are 1.5 it says 3 over 2. you say give me rats of 0.5 it says one over two say rats of three point two he writes it as 16 over 5 and so on factorial so it will give you the factorial of integer x so if x is 6 factorial of 6 is nothing but 6 divide multiply by 5 then multiply by 4 and multiply by 3 multiply by 2 and multiply by one that would be 720 so factorial gives you the factorial of an integer and choose k will give you number of possible combinations of k items taken from a group of n items so for example if you want to find the number of possible subgroups of 3 out of a group of 10 items then you use and choose k parentheses 10 comma 3 and that will give you 120 in this case you have 120 possible combinations of picking three items from a group of 10 items okay and the next two primes x it gives you the prime numbers that are less than x all of the prime numbers that are less than x would be expressed for you so when you say prime 10 we know 2 is the first one 3 is the next 5 is the next and 7 is the next those are are prime numbers and then is prime x basically looks at x and says whether that's a prime number or not if it's a prime number it gives you an answer of one and if it's not it will give you an answer of zero okay discrete math functions all right so we talked about many functions so far and we'll be using them as the time goes by but i like to stop discussing functions and talk about more you know concepts that enable you to write a matlab program yourself so you learn last time how you get input into a program by using input function now this part here that i'd like to discuss with you is how you display how you make your program to display a number or the result of an operation or a message for example to the screen to send a message to the screen we use a function for that purpose called display function disp so let's say you have a value that is this large here i'm sorry is that what we're discussing here i think the banner is covered yes so let's talk about data format before the display itself so when you have a number such as this one that has some real part in this case 12 and then it has another real part but it's the decimal part this one here we know matlab can process or process up to 15 decimal places so this number if you pay attention has really 15 decimal places okay so then for this number here there are different ways of having it displayed but not exactly the same as uh the value itself but in a different format there are various formats that we can use we can use format short this is the default format in matlab which means if you type in this number value and say value you would see or look at your workspace you will see that value is displayed with only four decimals so three four five and six but as you can see i'm saying seven to the last decimal that's because the number after that is larger than uh 0.5 by larger than 5 and therefore it adds 1 to the decimal before it and drops out so then that six becomes seven here that's called short format only two decimals then we have long format that displays the number exactly as is with 15 decimals and then we have short e which really displays the number in scientific notation so this is scientific notion notation and it's written this way the actual number is converted to a value between 1 and 10. so 12 converts to a number between 1 and 10 is 1.2 right and then the other decimals follow so then scientific number takes that value 1.2 and then other decimals up to 4 because it's short e so up to four decimals and then it multiplies it by 10. and he writes it this way it says e plus 0 0 1. so e is nothing but 10 to the power of 1. and in a sense this is a short format for scientific notation we could have scientific notation that is long also number is again converted to a value between 1 and 10 with 15 decimals and then plus e plus 0 0 1 so that's 10 to the power of 1. so remember this e here is not base of natural log is just an annotation for scientific notation when you express your number in scientific notation we also have short g that displays your value with three decimals we have also long g that displays your number with decimals 2 4 6 8 10 12 13 remember this is only 13 decimals with long g it's not 15. then we have rat which expresses your number in fraction so that 12.34 and so on is 1000 over 81. and format long engineering um short engineering engineering is very much like scientific notation however instead of one decimal one whole uh in the you know the actual whole part of the number which is the value on the left side so in scientific notation that value is a number between 1 and 10. in engineering it's a number between 10 and 100. and therefore 12 point something is written as 12.3457 because it's short engineering so four decimal and then says e to the power of 0 10 to the power of 0 and then you also have long which gives you all the decimals and 0 here so those are different ways of displaying your numbers and as i said matlab by default displays your number with uh displays your number uh with four decimal parts so now let's look at the actual display function that allows us to display value of numbers in whatever format we want and also how you send message for display message so the isp function inside the parenthesis you put the name of an array it displays the value of the array in command window now that array could be just an array of characters such as this one here where you put inside apostrophes hello display hello it displays hello to the comma in the command window you could say display five five is an array two it's a one row by one column array it's a scalar so it displays that value you could also put an array of characters so you have then brackets because this array has two elements so the first element which is a string of characters is separated comma from the second element which is itself also a string of character and then this results displayed results putting each element on the command line so it prints build kent and a blank after t so bill can and blank after c and then university that follows you could also define a variable like name and its alpha and then you can say display an array so now we have a square bracket inside pair of parentheses this is square bracket so it means what's inside is an array remember all elements are really what have to be the same type so if it's numeric everything has to be numeric if it's character everything has to be character so in this case we have first element which is hello and a blank and then next element which is name we already defined as output so then this results in printing hello out there in the command line so the array has to have all its elements of the same type as i said therefore you may not mix for example text and numbers side by side you can only do those if you turn the actual number into a string and that's where num to a string and end to a string function comes into play these functions tend a number with decimal parts so num to a string or a number that is integer into a string it turns those into a set of characters and then you can combine it with a string of character so let's look at that one here so these function convert a numeric value to a set of characters that can then be combined with the text for display purpose so let's take this one here we're defining an array of characters the first element of array is character one and six so it's not number 16 num to a string turns number 16 into character 1 and 6. and then the second element is sorry the second element is dash feb dash and the third element is also a character happened to be a number 2004 that we converted to a string of characters what are those string of characters its character 2 character 0 character 0 and character 4. so then the display of this results in this output on the command window says 16 dash february dash 2004 you could have a number 23.11 and you could just say bracket answer equals a blanket space or no then apostrophe around it comma and then you tell that number two three point one one to character two and three and dot and character one and character one so then it combines those two and then prints on a screen here answer two three dot one one all right so this another one if you say take x and convert it to a set of a string but take only integer part of that so you use into a string rather than num to a string so this function drops out any decimal that you have and only takes the integer part or the whole part in this case two and three and therefore when it displays it just displays 23 for us okay any questions so far all right so let's move on to another example here so you could see application of what you have learned how to write a script on your own and to you today you will be writing a script or a program script and program are the same thing really matlab calls it the string programmers call it the program okay so we are given this problem here in this problem uh it says that there is an equation called classes collaborate that is listed here p equals 6.11 a natural log base here to the power of delta h sub v over r then multiplied by 1 over 271 minus 1 over t and the parentheses close this function will give you saturation pressure of the air which you can then use that saturation pressure um for finding the humidity inside the air so it's used quite often and it's an essential and very reliable method of predicting humidity in the air for finding the humidity in the air you need the temperature of the air so that's this t here but this temperature is not in celsius or in fahrenheit this temperature is in kelvin and in order to get kelvin you can take fahrenheit and add 460 about 460 to it and if you have celsius you can take the celsius and add 273 to it and that will give you kelvin okay so there are different ways of getting that r is what we refer to as the gas constant of air it is a fixed number in physics and it's related to what type of gas you have each gas has a different gas constant it has to do with its molecular weight so it's directly related to that it's a fixed number in this case for air is 461 joule over kilogram okay and then delta h is how much heat you have to give to liquid water to make it all evaporate that heat is referred to as the latent heat of vaporization so how much energy you have to give to liquid water in order to evaporate that number is 200 i mean 2 453 kilojoule per kilogram so then we are asked to find saturation pressure of the air for a temperature range ranging from minimum to a maximum with 10 degrees increment between those two limits you're told to write a script that would enable us to calculate that pressure and we want the answers to be printed in a tabular form to be displayed in the command window in a tabular form where the first column of that table is the temperature and the second column of that is the pressure okay so if you were given a problem like this the way you think about this and you know come up with a strategy of writing the program is for the time being you want to always start your program with listing all the constants and then the next thing you want to do getting all inputs into the program so in this case here for example temperature it's minimum and its maximum has to be given by the user so then you write the statements that get all these inputs so now we have constants defined and then inputs obtained and then we write a statements related to the identification of the output values in this case the output value that we are interested in is pressure so then we write that a statement to calculate the pressure already given all these other components so the constants are defined the temperature is defined and then we can write this a statement that will give you the output okay so don't put this at the very top of your program this has to come after all these parameters are defined before it so delta h has to be defined r has to be defined and t has to be defined before you can even write this as statement that's why i said constants inputs and then a statement and once you finish with those if statements and outputs are defined then you have output statements so constants inputs mathematical if statements and then the output of statements don't forget that constant input statements outputs okay so with that in mind we can start writing our statement our script our script naturally each script has a comment section so i write my comments section in this case it's just definition of what this program is doing okay then i begin by cleaning my clearing my command clc and then clearing my workspace see clear the comma between them and then i use format compact remember this allows us to see more lines on the command line rather than uh you know sporadic space between lines of output we begin with all inputs i should have done constants but constants could also come with after input doesn't matter so i'm getting the maximum value of t and using input function remember it function requires a message to the screen so i'm saying enter upper range of temperature in fahrenheit apostrophe and then i get my minimum value of t send another input message to the screen enter lower range of temperature in very high once i have the lower range and upper range and i know the increment which was in given in the assignment 10 degrees i can define my temperature so i call this variable temp f i put f for fahrenheit not to forget that this is fahrenheit so then i have an array of numbers starts from the minimum value that was defined here minimum t colon 10 is the increment going all the way to the maximum value that was defined max of t so this is the temperature range in fahrenheit we convert that to kelvin remember we wanted that in kelvin in order to do that we have to add 459.6 to it so that becomes rankine and then we have to divide that by 1.8 to get kelvin okay all these numbers will be given to you if we intended you to write this program they're 459.6 and division by 0.8 but you know from physics we know that and i'm just writing that in so the next thing is now we're defining constants so delta h writing delta h and i'm writing that in scientific notation e6 and r value 4 6 1 461 and now we do our calculations i write my equation here 6.1 multiplied by exponent that was e first of all we divide delta h by r and then the result of that will be multiplied by another parenthesis that parenthesis would be calculated before we do the multiplication so is in that parenthesis you're dividing one by 270 you want three minus one divided by 10 k temperature in kelvin pay attention that is a dot here because temperature k is now an array and division by array is requiring dot operator so then we display the outputs as required so first of all we form an array that has two columns we call it my results because we want to form a table right the first column of that table said in the assignment has to be temperature so we take the temperature which is a raw array why is it the raw array because we're writing it as a row array this is how the colon operator works it creates a row array so then we put it in a column orientation so that's my first column comma then we want the pressure to be on the next column because temperature f and temperature k as a result of that where row array p becomes also a row array because temp k is in there and therefore we put transpose of p p apostrophe becomes a column array and we put it on the second column here so now my results contains two columns of data so forming columns is very simple i'm forming tables very simple if i had a three column table i put another comma here and put the three column variable right there transposed okay all right but do we want to give the table a header maybe some words that we know what these two columns are yeah we could do that i'm putting a blank space by using display apostrophe blank blank blank apostrophe so it leaves kind of a blank between the program and the output then i'm writing on the two columns temperature and pressure temperature and pressure so first column represent temperature that's the header for temperature and pressure is the column for pressure and then before the numbers appear i put the underlines here so that underline goes under temperature the second underline goes under the pressure and then i'll print my results oh i also put the units of the two columns in the next uh row so i say fahrenheit is the first one and that one millibar is the neck is the pressure we are using disp function for all of it and now we display our results so display my result so what we're going to see then is would be this here the sample input output is given here the first thing that happens is the input function that we saw the first input function here this message is going to is displayed enter upper range of temperature in value and then the next one and so on but when the first message is displayed the program waits for us to enter a number so that's why this happens once that message comes up i type in 100 for my minimum and then when the that was my maximum sorry upper range and then for the lower range i type 10 so that's 10 degrees fahrenheit and then my outputs are going my display functions are going to be displayed for me so execution takes place pressure would be obtained and now display function displays the header displays the underscore on their lines displays that units and then displays the columns of the two arrays which are these columns it prints naturally temperature with four decimals and pressure also with four decimals and it goes from the minimum 10 all the way to maximum 100 incremented each time by 10 degrees if you wanted to here for example three decimals we have to type in format what format g remember format g will give you three decimals all right any questions about how this program was written so as you can see constants and inputs first and then calculations and then output so as you write to your program today you would use this format comment your comment contains your name your assignment number and if you want to write a sentence or two as what the program is going to do that's fine and then you write your program in this all right so before we finish up today there are some uh miscellaneous items that i'd like to discuss and they are important items really then that's why i like to cover that here the first miscellaneous item that i like to discuss are special values special values are important in math lab and we want to make sure that those values stay the way they are a number of these predefined special values can be used in our program and they are already initialized in matlab and we can use them the way they are we try not to overwrite those values because they become useless from that point on we cannot go back and use them so we don't overwrite those by defining variables with the name of these special values so remember then if you take the name of a special variable and define it as a variable of your own meaning to assign it a value yourself then its default value cannot be used later so for example pi pi is a special value it represents 3.14159 more decimals of course but just approximately and therefore i can use it to find maybe the perimeter of a of a circle two pi r so multiply two by pi by ten so pi is that one and we're not going to say before that pi equal to 4 or pi equal to 5 because its actual value then is going to be 4 or 5 and no longer pi would be recognized as our special value okay so we have pi as i mentioned represents pi and it be represented in matlab with 15 decimal of different decimal digits we have special values i and j and if you use them it means a square root of minus 1. we have a special value that you may see in your error messages inf it means infinity when we divide a number by zero we get that infinity or error message inf n-a-n not a number not a number okay so when we divide zero by zero for example the answer is not a number and you get that message not a number a clock allows you to get the current date and time it comes in an array of six elements and it would be displaying year month day hour minute and seconds date so for example in your program at the very top if you type clock when you look at your command line you see what you see the date year month day hour minute and second that shows exactly when your program was executed so where i have clc and clear remember in the previous example that we clear the command and clear the workspace if i put clock after that and then write my the other stuff then this clock would be printed on your output on your command window and when you copy and paste your entire command window to the bottom of your program and you make a comment block as we discussed last time and then i not only see your input output sample but i can see exactly at what time and year month day hour minute and second you tested your program and you obtained those outputs a date is when you only want the date so it prints the date as day month and year eps is for epsilon epsilon is the smallest difference between two numbers and its value here is here two to two point some decimal times 10 to the power of minus 18. so it's very a small number and we also have seen a and s this is uh this stores the result of an expression if it's not assigned to a variable as you write your program you will be looking at some errors i'd like you to be aware that there are different types of error the first type of error has to do with syntax of your programs called syntax error so when you type in the name of a function like sine function s-i-n if you type s-e-n you get a syntax error it says this function name is not correct so we'll check for a spelling and punctuations that we may have then we have runtime error this is when you run your program and you get these types of errors for these types of error we have to check out input data that's the most common source of error for example you may have a parameter that you're reading as a as a number but later on you treat it as a set of characters and then you get an error message telling you that hey this variable is not containing the correct data type so run type error in order to see this in more detail the more more description about the error remove the semicolon at the end of your statement or use display to display the result of that variable so you could look at it and by looking at it recognize what is the problem or you could look at the workspace see what that whether that value is assigned a number and see that number right there so then there are also logical errors these are logical errors in their statements that you write for example you put to a multiplication and a division operators side by side and you get those types of verbs also check for typos in this case and check units as well for logical errors and if you have any problem just ask me all right there are some useful uh commands that i'd like to uh also show you here there's a command called help and that help enables you to help get help on any uh function name that you want and the way you write it is very simple you write help on the command window help space the name of the function and then the program gives you a list a long list of description of what that function does and even examples of how to use so help and then the name of the function but you could also use look for look for look for enables you to get description of a keyboard so if you want to see what the switch is you say look for switch it gives you a description of that keyword switch and you get some understanding of it typing which on the command window will give you which version of matlab we use clear you already know clears the workspace clc clears the command and then you can also use diary as a command but you put a file name and what it does it sends your output to the to that file name rather than sending it to the command window and then you can turn the diary off after you've used it or you could turn it on we also have who and whose that only lists the content of our workspace for us so we could see that and there is also more that enables or disables page output so when you look at matlab for example in [Music] on a screen here it just keeps going right if you want that also happen to your file for example where you transfer your output you put more and you say on and it results in a page by page display of your output and then you have you can use also this combination of keys if your program is kind of locked you see you you keep asking for run run run and nothing happens it's because there is something happening on the command window that is waiting for your response and you may not recognize that so the easiest way of getting rid of this block is to go to the command window click on the command window to activate that and then use the control plus letter c keys combinations one time or maybe repeated time until on the command line you see those two greater sign you see that the command line is now clear then you have removed that block and aborted the operation that caused that blockage and then by pressing run then you could see what is happening if that blockage appears again if then you could there would be messages that for you to see okay all right and finally if you have a long statement to write let's say you put display and you want a long name a long prompt to be displayed for example to the screen and the screen because is not you know the screen kind of scrolls to the right and as you're typing you don't see the rest of your program then you could uh take advantage of this continuation symbol which just three dots what it does is as you start writing and you get to a point where you see the length of your sentence is good enough then you can put three dots after that and continue on the next line whatever you want it to appear on the previous line and once you're done with that then it considers the next line as continuation of the line that you had the three dots added to okay we'll work on this also in more detail any questions folks before we start anything at all all right so that was our discussion for today i'd like to uh share our screen to discuss the program that you will be doing just one minute not too long ago okay so this is our home page we're going to announce to assignments and assignment number two we pull it up ask for the assignment you could download it also if you want so it's giving us a robot arm that has two linkage the first linkage is l1 and the second one is l2 and the hand is really at the very top here there are two motors that would allow this robot arm to function there is a base motor at the very bottom and there is an elbow motor at the in the middle that allows the upper part of the hand actually of the arm to move and then as these linkages basically l1 and l2 move at different angles here the robot can reach different locations on x y coordinate system okay the location of the hand here the tip of the hand um or where the actual linkage 2 is connected to the hand that location has a coordinate x and y and we can obtain that location by these two expressions remember angle theta 1 and theta 2 can be in radian in which case you use the cosine function and sine function or it could be in degrees in which case you say cosine d or sine d function you put a d for degrees so you're asked to write a script to calculate and display x and y location of the hand in this script you have to write it in a way that the user would be entering the value of the angles the value of the length of those two linkages l1 and l2 so you would have to use input statements to get these values now once you finish with that and you print your output which would be x and y outputs or x and y then you are going to be testing your program and we want you then to test your program among other things for these values given here l125 inches l2 18 inches data 130 degrees and theta 2 15 degrees so once you test it and you get your result on the command window remember what you did in the first assignment you copy paste that portion of the command window to the bottom of your script file you make it a comment by putting percentage in front of all the lines and then you save your script and you upload that script into assignment to folder just what we did last time any questions folks very simple don't forget input con and constants we don't have constants in this but input then your calculations or statements for calculations and then your output display of your output just those steps anything else folks when is this too um the due date is given i believe it's on friday okay if it's on friday yeah or saturday i think let me just take a look here and see what we're asking you okay yes it is due friday by midnight sounds good all right okay folks is anybody having any trouble uh