1 Matrix As the name implies, we want to mainly work with matrices using MATLAB. In MATLAB, [ 1 2 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] define the same row vector. I recommend you to use the last one...

1 answer below »
please complete these assignments. directions are fairly clear


1 Matrix As the name implies, we want to mainly work with matrices using MATLAB. In MATLAB, [ 1 2 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] define the same row vector. I recommend you to use the last one since this is easier to read. If you choose to use different expression, just make sure you are consistent (keeping your expressions consistent is beneficial in many ways). To start a new column, we use ‘;’ or space. [ 1 ; 2 ; 3 ] , [ 1 ; 2 ; 3 ] , [ 1 2 3 ] define the same column vector (If you put ; at the end of the command, it suppresses the result). When you have two row vectors, A and B, and you want to multiply them together, you can do A*B’ MATLAB has interesting feature called ‘elementary-wise computation’. For example, [ 1 , 2 ; 3 , 4 ] . * [ 1 , 2 ] returns [1, 4; 3, 8]. Try to play with elementary-wise multiplication a little bit and see what happens. You can also do elementary-wise exponentiation in a similar fashion. [ 1 , 2 ] . ˆ 2 returns [1, 4]. Note that without ‘.’ the computation above is not possible, therefore MATLAB would return an error. Here are some useful commands. ˆ inv(A): If A is invertible, this computes the inverse of A. ˆ eye(N): Define an N by N identity matrix. ˆ eye(M,N): Define an M by N matrix with 1’s in diagonals and zeros elsewhere. ˆ zeros(M,N): Define an M by N zero matrix. ˆ ones(M,N): Define an M by N matrix with all ones. ˆ M:N or [M:N]: Define a row vector of size M-N+1. It will start at M and increase by 1 up to N. ˆ M:L:N or [M:L:N]: It will start at M and increase by L up to N. ˆ A(N): N-th entry of A. It counts columns first. ˆ A(n,m): An element in the n-th row and m-th column. ˆ size(A): Size of A in each dimension. ˆ numel(A): Number of elements in A. ˆ legnth(A): The size of the biggest dimension of A. 2 Manipulating matrices Let’s see how we can change elements of a matrix. For example, if we want to change the n-th element of A, we simply do A(n)=m. We can also change multiple elements at a time. A(2:4)=[1, 2, 3] will change the second, third and fourth elements of the matrix A to 1, 2 and 3 respectively. Similarlry, A([1, 3, 4])=[1, 2, 3] will change the first, third and fourth elements of the matrix A to 1, 2 and 3 respectively. We can use similar tricks to create a big matrix. For example, A=[1:5; 6:10; 11:15; 16:20; 21:25] will create a 5 by 5 matrix, each row starting with 1, 6, 11, 16, 21. A(1:2, 3:5) is 2 by 3 submatrix of A taking the first row, second row and third column, fourth column, fifth column. A([1, 5], [1, 5]) gives a 2 by 2 matrix with corners of A. You can apply these further to manipulate matrices. 1 3 Strings MATLAB deals with strings in somewhat interesting way. In MATLAB, a string of multiple characters is considered as a list of characters. For example, c=’ l e t t e r ’ defines the variable c to be a list of character l, e, t, t, e, r. Why does this matter? As you know, computers only use binary numbers. That is, ‘l’ is stored as a number in the computer. Therefore, we can do somewhat meaningful computations between strings and numbers and that’s exactly what MATLAB does. For example, ’2’+2 returns 52 because the string 2 has the value 50. How does MATLAB decide which value each string takes? It uses American Standard Code for Information Interchange, known as ASCII. You can find the table of values of printable characters here. Thus, ‘c’ we defined above will be treated as the list [108 101 116 116 101 114] when we ask MATLAB to do some algebraic computation on it (I highly recommend you to read about ASCII since this is a very important concept in computer science). We can change a number to a string as well by using ‘num2str()’ command. This can be used when you want to print a text including some results. For example, if X is the result, you can type [’The result is ’ num2str(X)]. 4 Conditionals Conditionals in MATLAB is quite similar to that of *nix. i f cond i t i on1 do something e l s e i f cond i t i on2 %%op t i ona l do something else %%op t i ona l do something end is the basic structure. ‘elseif’, ‘else’ parts are optional and % starts a comment. The relation operators are ˆ <: less="" than="" ˆ=""><=: less="" than="" or="" equal="" to="" ˆ="">: greater than ˆ >=: greater than or equal to ˆ ==: equal to ˆ ∼=: not equal to 5 For loops for loop repeats the same operation certain amount of time. The basic structure is for i =1:N do th ings end performs ‘do things’ for i=1, 2,· · · , N. We can use similar tricks as before here as well. for i =1:2 :5 do th ings end performs ‘do things’ for i=1, 3, 5. 2 https://en.wikipedia.org/wiki/ASCII 6 While loops While loop repeats the same operation ‘while’ certain condition is met. while cond i t i on do th ings end is the basic structure of a while loop. This will repeat the command while the condition is true. You can almost always if not always use while loops instead of for loops. However, for loops can be much faster than while loops since while loops require the program to check the condition each time it finishes a loop which can be very time consuming if it’s repeated many times. Similarly, you can use for loops and conditionals to replace while loops. 7 Script When you write a longer code, you probably want to create a script. In ‘home’ tab, you can find ‘New Script’ option to start a new script. When you run a script, MATLAB will execute every command in the script at once. MATLAB prints every result which you may not want. If you want to suppress certain results, you can add ‘;’ at the end. You may want to put ‘clear’ in the beginning of the script. This basically clears out all saved variables and prevents the possible affects from previous results. Of course, you need be careful if you are running multiple scripts at a time. 8 Assignment 1. Start a new script. 2. Write a code that will convert your birthday(YYYY, MM, DD) into binary numbers. 3. Print the result as ‘My birthday in binary system is YYYY, MM, DD.’ 4. Upload the script to Blackboard. Some functions that can be very useful for this assignment. ˆ rem(a,b): remainder after division. ˆ floor(m): the biggest integer smaller than m. 3 Matrix Manipulating matrices Strings Conditionals For loops While loops Script Assignment 1 Display format In MATLAB, you can change display settings. • format long: Long, fixed-decimal format with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values. • format shortEng: Short engineering notation (exponent is a multiple of 3) with 4 digits after the decimal point. • format longG:Long, fixed-decimal format or scientific notation, whichever is more compact, with a total of 15 digits for double values, and 7 digits for single values. • format compact: Suppress excess blank lines to show more output on a single screen. There are many other display options. You can search ‘format’ in help. 2 User input Sometimes, you want to ask whoever is running the script to enter a value for a certain variable. In that case, you can use ‘input’ command. x = input ( prompt ) This will display ‘input’ as a guideline and whatever you input becomes ‘x’. 3 Riemann integral Let f(x) be a function that is integrable in an interval I = [a, b]. By using the definition of Riemann integral, we can estimate the integral of f(x) in the interval I by m∑ n=0 f ( xn+1 + xn 2 ) (xn+1 − xn) where xn < xn+1 for n = 0, 1, · · · ,m− 1. we normally make this partition evenly spaced but that’s quite boring. so let’s say x = (a = x0, x1, · · · , xm = b) is the partition we are going to use to estimate∫ b a f(x)dx. for the sake of simplicity, i’ll use f(x) = sinx and i = [0, 1]. r =0; %%t h i s w i l l be my r e s u l t . x=[0 , 0 . 1 , 0 . 3 , 0 . 34 , 0 . 54 , 0 . 6 , 0 . 66 , 0 . 73 , 0 . 8 , 0 . 9 , 0 . 93 , 1 ] ; %%pa r t i t i o n for i =1: length ( x)−1 %%s t a r t o f a f o r loop . r=r+sin ( ( x ( i +1)+x ( i ) ) / 2 )∗ ( x ( i +1)−x ( i ) ) ; i %%this i s a check po in t . end [ ’the e s t imat i on o f the i n t e g r a l i s ’ num2str( r ) ] %%pr i n t i n g the r e s u l t . 4 assignment let’s try something more complicated. what should we do if we want to calculate the minimum number of partitions to estimate the integral within certain error? write a script that does the following: • starting number of partition is n = 10 (evenly spaced). • estimate ∫ 1 0 exdx using this partition. 1 • calculate the error. • if the error is bigger than 10−5, increase the number of partition by 1. • if the error is smaller than 10−5 stop the process. • print the minimum number of partition needed. • upload your matlab script in blackboard. here is a suggestion xn+1="" for="" n="0," 1,="" ·="" ·="" ·="" ,m−="" 1.="" we="" normally="" make="" this="" partition="" evenly="" spaced="" but="" that’s="" quite="" boring.="" so="" let’s="" say="" x="(a" =="" x0,="" x1,="" ·="" ·="" ·="" ,="" xm="b)" is="" the="" partition="" we="" are="" going="" to="" use="" to="" estimate∫="" b="" a="" f(x)dx.="" for="" the="" sake="" of="" simplicity,="" i’ll="" use="" f(x)="sinx" and="" i="[0," 1].="" r="0;" %%t="" h="" i="" s="" w="" i="" l="" l="" be="" my="" r="" e="" s="" u="" l="" t="" .="" x="[0" ,="" 0="" .="" 1="" ,="" 0="" .="" 3="" ,="" 0="" .="" 34="" ,="" 0="" .="" 54="" ,="" 0="" .="" 6="" ,="" 0="" .="" 66="" ,="" 0="" .="" 73="" ,="" 0="" .="" 8="" ,="" 0="" .="" 9="" ,="" 0="" .="" 93="" ,="" 1="" ]="" ;="" %%pa="" r="" t="" i="" t="" i="" o="" n="" for="" i="1:" length="" (="" x)−1="" %%s="" t="" a="" r="" t="" o="" f="" a="" f="" o="" r="" loop="" .="" r="r+sin" (="" (="" x="" (="" i="" +1)+x="" (="" i="" )="" )="" 2="" )∗="" (="" x="" (="" i="" +1)−x="" (="" i="" )="" )="" ;="" i="" %%this="" i="" s="" a="" check="" po="" in="" t="" .="" end="" [="" ’the="" e="" s="" t="" imat="" i="" on="" o="" f="" the="" i="" n="" t="" e="" g="" r="" a="" l="" i="" s="" ’="" num2str(="" r="" )="" ]="" %%pr="" i="" n="" t="" i="" n="" g="" the="" r="" e="" s="" u="" l="" t="" .="" 4="" assignment="" let’s="" try="" something="" more="" complicated.="" what="" should="" we="" do="" if="" we="" want="" to="" calculate="" the="" minimum="" number="" of="" partitions="" to="" estimate="" the="" integral="" within="" certain="" error?="" write="" a="" script="" that="" does="" the="" following:="" •="" starting="" number="" of="" partition="" is="" n="10" (evenly="" spaced).="" •="" estimate="" ∫="" 1="" 0="" exdx="" using="" this="" partition.="" 1="" •="" calculate="" the="" error.="" •="" if="" the="" error="" is="" bigger="" than="" 10−5,="" increase="" the="" number="" of="" partition="" by="" 1.="" •="" if="" the="" error="" is="" smaller="" than="" 10−5="" stop="" the="" process.="" •="" print="" the="" minimum="" number="" of="" partition="" needed.="" •="" upload="" your="" matlab="" script="" in="" blackboard.="" here="" is="" a="">
Answered 3 days AfterOct 23, 2021

Answer To: 1 Matrix As the name implies, we want to mainly work with matrices using MATLAB. In MATLAB, [ 1 2 3...

Mathews answered on Oct 27 2021
111 Votes
close all
clear all
clc
y=input('enter year of birth');
m=input('enter month of birth');
d=inpu
t('enter day of birth');
yy=dec2bin(y)
mm=dec2bin(m)
dd=dec2bin(d)
x=['Binary form of date of birth ',num2str(d),' - ',num2str(m),' - ',num2str(y),' = ', num2str(dd),' - ',num2str(mm),' - ',...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here