Loading...

Messages

Proposals

Stuck in your homework and missing deadline? Get urgent help in $10/Page with 24 hours deadline

Get Urgent Writing Help In Your Essays, Assignments, Homeworks, Dissertation, Thesis Or Coursework & Achieve A+ Grades.

Privacy Guaranteed - 100% Plagiarism Free Writing - Free Turnitin Report - Professional And Experienced Writers - 24/7 Online Support

Matlab for loop geometric series

01/12/2021 Client: muhammad11 Deadline: 2 Day

MATLAB sessions: Laboratory 2

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

In this laboratory session we will learn how to

1. Create and manipulate matrices and vectors.

2. Write simple programs in MATLAB

NOTE: For your lab write-up, follow the instructions of LAB1.

Matrices and Linear Algebra

! Matrices can be constructed in MATLAB in di!erent ways. For example the 3 ! 3 matrix

A =

!

" 8 1 6 3 5 7 4 9 2

#

$ can be entered as

>> A=[8,1,6;3,5,7;4,9,2] A =

8 1 6 3 5 7 4 9 2

or

>> A=[8,1,6; 3,5,7; 4,9,2] A =

8 1 6 3 5 7 4 9 2

or defined as the concatenation of 3 rows

>> row1=[8,1,6]; row2=[3,5,7]; row3=[4,9,2]; A=[row1;row2;row3] A =

8 1 6 3 5 7 4 9 2

or 3 columns

>> col1=[8;3;4]; col2=[1;5;9]; col3=[6;7;2]; A=[col1,col2,col3] A =

8 1 6 3 5 7 4 9 2

Note the use of , and ;. Concatenated rows/columns must have the same length. Larger matrices can be created from smaller ones in the same way:

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

>> C=[A,A] % Same as C=[A A] C =

8 1 6 8 1 6 3 5 7 3 5 7 4 9 2 4 9 2

The matrix C has dimension 3 ! 6 (“3 by 6”). On the other hand smaller matrices (submatrices) can be extracted from any given matrix:

>> A(2,3) % coefficient of A in 2nd row, 3rd column ans =

7 >> A(1,:) % 1st row of A ans =

8 1 6 >> A(:,3) % 3rd column of A ans =

6 7 2

>> A([1,3],[2,3]) % keep coefficients in rows 1 & 3 and columns 2 & 3 ans =

1 6 9 2

! Some matrices are already predefined in MATLAB:

>> I=eye(3) % the Identity matrix I =

1 0 0 0 1 0 0 0 1

>> magic(3) ans =

8 1 6 3 5 7 4 9 2

(what is magic about this matrix?) ! Matrices can be manipulated very easily in MATLAB (unlike Maple). Here are sample commands to exercise with:

>> A=magic(3); >> B=A’ % transpose of A, i.e, rows of B are columns of A B =

8 3 4 1 5 9 6 7 2

>> A+B % sum of A and B ans =

16 4 10 4 10 16 10 16 4

>> A*B % standard linear algebra matrix multiplication ans =

101 71 53

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

71 83 71 53 71 101

>> A.*B % coefficient-wise multiplication ans =

64 3 24 3 25 63 24 63 4

! One MATLAB command is especially relevant when studying the solution of linear systems of dif- ferentials equations: x=A\b determines the solution x = A!1b of the linear system Ax = b. Here is an example:

>> A=magic(3); >> z=[1,2,3]’ % same as z=[1;2;3] z =

1 2 3

>> b=A*z b = 28 34 28

>> x = A\b % solve the system Ax = b. Compare with the exact solution, z, defined above. x = 1 2 3 >> y =inv(A)*b % solve the system using the inverse: less efficient and accurate ans = 1.0000 2.0000 3.0000

Now let’s check for accuracy by evaluating the di!erence z # x and z # y. In exact arithmetic they should both be zero since x, y and z all represent the solution to the system.

>> z - x % error for backslash command ans =

0 0 0

>> z - y % error for inverse ans = 1.0e-015 * -0.4441

0 -0.8882

Note the multiplicative factor 10!15 in the last computation. MATLAB performs all operations using standard IEEE double precision.

Important!: Because of the finite precision of computer arithmetic and roundo! error, vectors or matrices that are zero (theoretically) may appear in MATLAB in exponential form such as 1.0e-15 M where M is a vector or matrix with entries between #1 and 1. This means that each component of the

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

answer is less than 10!15 in absolute value, so the vector or matrix can be treated as zero (numerically) in comparison to vectors or matrices that are on the order of 1 in size.

EXERCISE 1 Enter the following matrices and vectors in MATLAB

A =

!

" 1 4 2 2 5 8 3 6 9

#

$ , B =

!

" 1 2 3 4 5 6 7 8 9

#

$ , b =

!

" 4 23 27

#

$ , c = % 4 3 2

& , d =

!

" 1 2 3

#

$

(a) Perform the following operations: AB, BA, cB and Ad (use standard linear algebra multiplica- tion).

(b) Construct a 3 ! 6 matrix C = [A B ] and a 4 ! 3 matrix D = '

B c

( .

(c) Use the “backslash” command to solve the system Ax = b.

(d) Replace A(2, 3) with 0.

(e) Extract the 3rd row of the matrix A.

(f) A row or a column of a matrix can be deleted by assigning the empty vector [] to the row or the column. For instance A(2,:)=[] deletes the second row of the matrix A. Delete the third row of the matrix B.

MATLAB Programming

It is often advantageous to be able to execute a segment of a code a number of times. A segment of a code that is executed repeatedly is called a loop.

To understand how loops work, it is important to recognize the di!erence between an algebraic equality and a MATLAB assignment. Consider the following commands:

>> counter = 2 counter =

2 >> counter = counter +1 counter =

3

The last statement does not say that counter is one more than itself. When MATLAB encounters the second statement, it looks up the present value of counter (2), evaluates the expression counter + 1 (3), and stores the result of the computation in the variable on the left, here counter. The e!ect of the statement is to increment the variable counter by 1, from 3 to 4.

Similarly, consider the commands:

>> v=[1,2,3] v =

1 2 3 >> v=[v,4] v =

1 2 3 4

When MATLAB encounters the second statement, it looks up the present value of v, adds the number 4 as entry of the vector, and stores the result in the variable on the left, here v. The e!ect of the statement is to augment the vector v with the entry 4.

There are two types of loops in MATLAB: for loops and while loops

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

for loops

When we know exactly how many times to execute the loop, the for loop is often a good implementation choice. One form of the command is as follows:

for k=kmin:kmax

end

The loop index or loop variable is k, and k takes on integer values from the loop’s initial value, kmin, through its terminal value, kmax. For each value of k, MATLAB executes the body of the loop, which is the list of commands.

Here are a few examples:

• Determine the sum of the squares of integers from 1 to 10: 12 + 22 + 32 + . . . + 102.

S = 0; % initialize running sum for k = 1:10

S = S+k^2; end S

Because we are not printing intermediate values of S, we display the final value of S after the loop by typing S on a line by itself. Try removing the “;” inside the loop to see how S is incremented every time we go through the loop.

• Determine the product of the integers from 1 to 10: 1 · 2 · 3 · . . . · 10.

p = 1; % initialize running product for k = 2:10

p = p*k; end p

! Whenever possible all these construct should be avoided and built in MATLAB functions used instead to improve e"ciency. In particular lengthy loops introduce a substantial overhead. The value of S in the example above can be evaluated with a single MATLAB statement:

>> S = sum((1:10).^2)

Type help sum to see how the built in sum function works.

Similarly the product p can be evaluated using

>> p = prod(1:10)

Type help prod to see how the built in prod function works.

EXERCISE 2 Recall that a geometric sum is a sum of the form a + ar + ar2 + ar3 + . . ..

(a) Write a function file that accepts the values of r, a and n as arguments and uses a for loop to return the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1/2 and n = 10.

(b) Write a function file that accepts the values of r, a and n as arguments and uses the built in command sum to find the sum of the first n terms of the geometric series. Test your function for a = 3, r = 1/2 and n = 10. Hint: Start by defining the vector e=0:n-1 and then evaluate the vector R = r.^e. It should be easy to figure out how to find the sum from there.

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

EXERCISE 3 The counter in a for or while loop can be given explicit increment: for i =m:k:n to advance the counter i by k each time. In this problem we will evaluate the product of the first 10 odd numbers 1 · 3 · 5 · . . . · 19 in two ways:

(a) Write a script file that evaluates the product of the first 10 odd numbers using a for loop.

(b) Evaluate the product of the first 10 odd numbers using a single MATLAB command. Use the MATLAB command prod.

while loop

The while loop repeats a sequence of commands as long as some condition is met. The basic structure of a while loop is the following:

while

end

Here are some examples:

• Determine the sum of the inverses of squares of integers from 1 until the inverse of the integer square is less than 10!10: 1

12 + 1

22 + . . . + 1

k2 while 1

k2 $ 10!10.

S = 0; % initialize running sum k = 1; % initialize current integer incr = 1; % initialize test value while incr>=1e-10

S = S+incr; k = k+1; incr = 1/k^2;

end

What is the value of S returned by this script? Compare to ")

k=1

1

k2 =

!2

6 .

• Create a row vector y that contains all the factorials below 2000: y = [ 1!, 2!, 3!, . . . k! ] while k! < 2000.

y = []; % initialize the vector y to the empty vector k = 1; % initialize the counter value = 1; % initialize the test value to be added to the vector y while value < 2000

y = [y, value]; % augment the vector y k = k+1; % update the counter value = factorial(k); % evaluate the next test value

end y

EXERCISE 4 Write a script file that creates a row vector v containing all the powers of 2 below 1000. The output vector should have the form: v = [ 2, 4, 8, 16 . . . ]. Use a while loop.

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi
naser alateeqi
naser alateeqi
naser alateeqi
MATLAB sessions: Laboratory 2

if statement

The basic structure of an if statement is the following:

if condition

elseif condition :

else

end

Here is an example:

• Evaluate

y =

* +

,

x3 + 2, x % 1 1

x # 2 , x > 1

for a given (but unknown) scalar x and, if x = 2, display “y is undefined at x = 2”.

function y=f(x) if x==2

disp(’y is undefined at x = 2’) elseif x <= 1

y=x^3+2; else

y=1/(x-2); end end

We can test the file by evaluating it at di!erent values of x. Below we evaluate the function at x = #1, x = 2 and x = 4.

>> f(-1) ans =

1 >> f(2) y is undefined at x = 2 >> f(4) ans =

0.5000

EXERCISE 5 Write a function file that creates the following piecewise function:

f(x) =

* --+

--,

x2 + 1, x % 3 ex, 3 < x % 5

x

x # 10 , x > 5

Assume x is a scalar. The function file should contain an if statement to distinguish between the di!erent cases. The function should also display “the function is undefined at x = 10” if the input is x = 10. Test your function by evaluating f(1), f(4), f(7) and f(10).

c"2011 Stefania Tracogna, SoMSS, ASU

naser alateeqi
naser alateeqi

Homework is Completed By:

Writer Writer Name Amount Client Comments & Rating
Instant Homework Helper

ONLINE

Instant Homework Helper

$36

She helped me in last minute in a very reasonable price. She is a lifesaver, I got A+ grade in my homework, I will surely hire her again for my next assignments, Thumbs Up!

Order & Get This Solution Within 3 Hours in $25/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 3 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

Order & Get This Solution Within 6 Hours in $20/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 6 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

Order & Get This Solution Within 12 Hours in $15/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 12 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

6 writers have sent their proposals to do this homework:

Study Master
Instant Assignments
Top Grade Essay
Top Essay Tutor
Math Exam Success
Smart Tutor
Writer Writer Name Offer Chat
Study Master

ONLINE

Study Master

As an experienced writer, I have extensive experience in business writing, report writing, business profile writing, writing business reports and business plans for my clients.

$21 Chat With Writer
Instant Assignments

ONLINE

Instant Assignments

I have written research reports, assignments, thesis, research proposals, and dissertations for different level students and on different subjects.

$41 Chat With Writer
Top Grade Essay

ONLINE

Top Grade Essay

I have assisted scholars, business persons, startups, entrepreneurs, marketers, managers etc in their, pitches, presentations, market research, business plans etc.

$24 Chat With Writer
Top Essay Tutor

ONLINE

Top Essay Tutor

I find your project quite stimulating and related to my profession. I can surely contribute you with your project.

$16 Chat With Writer
Math Exam Success

ONLINE

Math Exam Success

This project is my strength and I can fulfill your requirements properly within your given deadline. I always give plagiarism-free work to my clients at very competitive prices.

$32 Chat With Writer
Smart Tutor

ONLINE

Smart Tutor

I have written research reports, assignments, thesis, research proposals, and dissertations for different level students and on different subjects.

$28 Chat With Writer

Let our expert academic writers to help you in achieving a+ grades in your homework, assignment, quiz or exam.

Similar Homework Questions

Interturbine aviation logistics fze - Roman numerals crack the code - Statistics Memo - Ernie and bert thirsty - One page assignment - Wells fargo retail services phone number - Sea floor spreading lab activity - Breath holding spells rch - Posner v scott lewis - Why did ellie walker leave the andy griffith show - Trends & issues in executive management for health care administrators - Prepare Work for Your "Positive Structural Change" Project. - Jitterbug dance video clips - Gen y in the workforce case analysis - Australian tax office darwin - 3 page essay on team based leadership - Metarteriole and thoroughfare channel - Topic 4 review cwv - Warriors of the net review - Southwestern university stadium construction case study answer - More experience bigger brain rosenzweig - According to real business cycle theory - Wheel and axle ima - Scientific method lab report answers - Two stage cmos op amp - Informatics in healthcare - Write A Brief Summary Of The Unbecoming Behavior 400 Words Only - North richmond community health dental - Dr krishna tumuluri sydney - Catholic baptism signs and symbols - Haiyuan ningxia china dec 16 1920 - Oxidation and reduction lab report - Minimum 150 words The Romantic period was all about emotion and individual expression. How are the popular, social and political trends of that time similar to the trends of today? - A rink kink kink song - Acme expense report xlsx - Jdsu hst 3000 t1 testing - Discussion 7 - Kahlia chamberlain wedding photos - 6 3 submission identifying your thesis - Academic social networking sites ppt - Lattice enthalpy of formation - HR - High modality words list - SEXSOMNIAC - Neat people vs sloppy people summary response - Netflix strategy case study - Movie recommendation system report - What version of ip is present in frame 546? what is the source ip address? - Peter and amy jones tax return - Write briefly on cloud computing recommendations suggested by nist - Assignment - Bharatanatyam junior exam book - COVID-19 Environmental Impact Report - Multiplying and dividing scientific notation worksheet doc - Discussion question - Marco polo the explorer - DAT/565: Data Analysis And Business Analytics Wk 4 - Practice: Wk 4 Exercises Calculation of Total Std. Dev (one value only to be calculated) - Erecruit nsw health dates - Monotype corsiva history - New heritage doll company capital budgeting excel - Oceanview marine company 20 4 - The laramie project quotes - Chewy american candy crossword clue - Final Essay - Dirty jobs las vegas pig farm episode - Salkowski test for cholesterol - Where did the story the epic of gilgamesh originate - Mds business rules examples - How to prepare acidic buffer - For most new englanders indians represented - Week 3 quiz investments - Project 4 - When god closes a door he opens a window verse - Admn4900 - TEam project - A gas mixture contains hbr no2 and c2h6 at stp - Cascade apartments mt baw baw - Introduction to Medical Informatics - WEEK 2 DISCUSSION BUSINESS LAW 1 - Psy/203 version 5 week 1 review worksheet - Heat of neutralization lab report sheet - Why does length affect the period of a pendulum - Effects of social networking sites ppt - Bank account program in java using constructors - Multimedia university assignment cover page - Land air express of new england - Durham council planning portal - NURS561REPLYPROMPT1 - 14.2 choosing among linear quadratic and exponential models answers - Consumer buying behavior report example - Board of intermediate education karachi - I am very real kurt vonnegut - Humanized robots case study solution - A roulette wheel has 38 slots around the rim - Introducing new coke case study - 3.15 kg in lbs - Blt ln share price - Great is he chords - Akers social learning theory - 5 sources of power in organizations