Question
write matlab script

5. Use a for loop to sum the elements in x = [1 2 3 4 5] Check your answer with sum function 6. Use a while loop to sum the e
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab code:

5)Sum using for loop

//main code

x=[1,2,3,4,5];

y=0;

for n=1:length(x)

y=y+x(n);

end

//end of main code

y
y =
15

//main code

z=sum(x)

//end of main code
z =
15

As we can see the value of y and z are same.

y is calculated using for loop and z is calculated using sum function.

6)Sum using while loop

//main code

a=[1,2,3,4,5];

s=0;

i=1;

while i<=length(a)

s=s+arr(i);

i=i+1;

end

//end of main code

s
s =
15

//now we use sum function

//main code

z=sum(x)

//end of main code

z

z =
15

As we can see the value of s and z are same.

s is calculated using while loop and z is calculated using sum function.

Add a comment
Know the answer?
Add Answer to:
write matlab script 5. Use a for loop to sum the elements in x = [1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Flowcharts & for loops 1. Using a for loop, write a MatLab script to convert ounces...

    Flowcharts & for loops 1. Using a for loop, write a MatLab script to convert ounces to grams for 0 to 16 ounces, in 1-ounce increments. Present output in a table; label columns appropriately. 2. A Fibonacci sequence is composed of elements created by adding the previous 2 elements. The simplest Fibonacci sequence starts with 1, 1 and proceeds as follows: 1,1,2,3,5,8… Element 3 is the sum of elements 1 and 2 (1+1=2) Element 4 is the sum of elements...

  • Write a MATLAB script file that will sum all the positive entries in V and stop...

    Write a MATLAB script file that will sum all the positive entries in V and stop summing once the sum reaches or exceeds 20(while loop). An fprintf statement should be used to display the value for the sum to prove it has reached or exceeded 20, and the number of entries to reach this value should be displayed. V = [7 9 -8 9 3 -8 -5 1 10 10 0 -7] Reminder: don’t use the built-in function, sum. Use...

  • In MATLAB Write a script to determine the number of terms required for the sum of...

    In MATLAB Write a script to determine the number of terms required for the sum of the series (5k? – 2k) not to exceed 100,000 using the while loop. Display the number of terms and the calculated sum using fprint () command as follow: The number of terms is 38 and its sum is 93613

  • Solve in MATLAB Problem 3: Given the vector x- [2 1 0 6 2 3 5...

    Solve in MATLAB Problem 3: Given the vector x- [2 1 0 6 2 3 5 3 4 1 3 2 10 12 4 2 7 9 2 4 51 use a for loop to (a) Add up the values of all elements in x (b) Compute the cumulative sum, y, of elements in x You can check your results using the built-in functions sum and cumsum. Q.5 What is the value of the sum of elements in vector x?...

  • Write a MATLAB script to plot the function of f(x) given by: Write a MATLAB script...

    Write a MATLAB script to plot the function of f(x) given by: Write a MATLAB script to plot the function of f(x) given by: f(x) = integral x^2 - pi^2/4 x > pi/2 8 * cos x -pi/2 lessthanorequalto x lessthanorequalto pi/2 pi^2/4 - x^2 x < -pi/2

  • answer 4, 5, 6, 7 using matlab Homework 10 1. Write the correct first-line syntax for...

    answer 4, 5, 6, 7 using matlab Homework 10 1. Write the correct first-line syntax for a user defined function that is named Feynman and has input variables q and e, and outputs L and M. (Comment out of this line). 2. Using zeros, ones, and eye commands write the one line syntax to create the following matrix: 00000 01100 01010 01001 3. Write the syntax for the user defined function that calculates the surface area and volume of a...

  • Matlab Homework Please write a script that does the following: generates a 3 x 5 matrix...

    Matlab Homework Please write a script that does the following: generates a 3 x 5 matrix of random integersand call it A. generates a 3 x 3matrix of random integers and call it B. "Fix" A so it can be compared to B How do we "Fix" A? Make sure to reassign the value to A Tell me how many elements of A are greater than or equal to B This can be done in a single line. You will...

  • Write a MATLAB script, using either a single or nested for-loop, that will print the factorials...

    Write a MATLAB script, using either a single or nested for-loop, that will print the factorials for the numbers between 1 and 100 (inclusive). The factorial of n (n!) is the product of the positive integers less than or equal to n. For example: 3! = 3 * 2 * 1. For this question you cannot use the built-in MATLAB factorial function, but you can use other MATLAB functions if you wish. MATLAB code!MATLAB code!MATLAB code!MATLAB code!

  • Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos...

    Convince yourself that the Maclaurin Series for cos(x) is: A. Write a function script called cos_series that takes that takes as its inputs, x and N and has output given by the sum in the N-term Maclaurin Series approximation for Cos(x). Hint: try a “for loop” and set “format long” in your code. You may use the MATLAB built-in function factorial() B. Check your code by finding the 2-terms, 3-terms, 4-terms, 5-terms and 6-terms Maclaurin Series approximations every 30 degrees...

  • USE FOR LOOP INSTEAD OF WHILE LOOP Consider the following script file. Fill in the lines...

    USE FOR LOOP INSTEAD OF WHILE LOOP Consider the following script file. Fill in the lines of the following table with the values that would be displayed immediately after the while statement if you ran the script file. Write in the values the variables have each time the while statement is executed. You might need more or fewer lines in the table. Then type in the file, and run it to check your answers. k = 1; b = -2;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT