Question

Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...

Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a non-negative integer value a. The script should then evaluates the sum Pn i=1 i a using a running sum for the case when n = 5. More specifically, use a counter for i that is used in the output fprintf as well as in the running sum. Feel free to write some code and just repeat it 5 times in your script. Sample output: Enter a non-negative integer value for a: 7 i is 1, Sum of iˆ7 so far is 1 i is 2, Sum of iˆ7 so far is 129 i is 3, Sum of iˆ7 so far is 2316 i is 4, Sum of iˆ7 so far is 18700 i is 5, Sum of iˆ7 so far is 96825 Enter a non-negative integer value for a: 1 i is 1, Sum of iˆ1 so far is 1 i is 2, Sum of iˆ1 so far is 3 i is 3, Sum of iˆ1 so far is 6 i is 4, Sum of iˆ1 so far is 10 i is 5, Sum of iˆ1 so far is 15

Homework Answers

Answer #1

Code:

clc;clear all;
a=input('Enter a non-negative integer value for a:');
x=0;
for i=1:1:5
x=x+i^a;
fprintf('i is %d , Sum of iˆ%d so far is %d \n',i,a,x)
endfor

Command window:(output)

Enter a non-negative integer value for a:7
i is 1 , Sum of iˆ7 so far is 1
i is 2 , Sum of iˆ7 so far is 129
i is 3 , Sum of iˆ7 so far is 2316
i is 4 , Sum of iˆ7 so far is 18700
i is 5 , Sum of iˆ7 so far is 96825

Enter a non-negative integer value for a:1
i is 1 , Sum of iˆ1 so far is 1
i is 2 , Sum of iˆ1 so far is 3
i is 3 , Sum of iˆ1 so far is 6
i is 4 , Sum of iˆ1 so far is 10
i is 5 , Sum of iˆ1 so far is 15

Know the answer?
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
Similar Questions
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9,...
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Write a Python program that prompts a user for two integers and stores the two integers...
Write a Python program that prompts a user for two integers and stores the two integers the user types into the shell. Print the product, float division, integer division, remainder, sum, and difference of the two integers to the console. The entire equation must be printed (see below for formatting). Only display 2 digits after the decimal place for results that are floats. Include a module docstring that summarizes the program.. Sample Run 1: Enter an integer: 5 Enter another...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish the same as the following: matsum = sum(mat') Please make sure to store the result in "matsum" and dont use sum. This is what I have so far but it keeps telling me is wrong.... % mat has been initialized for you: mat = randi([-100,100],randi([15,20]),randi([15,20])); % write your statements that accomplish the code above: [r c]=size(mat); for i=1:c matsum=0; for j=1:r matsum=matsum+mat(j,i); end...
(Python Programming) Write a program that prompts a user for a positive integer and then uses...
(Python Programming) Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows: Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
I need MATLAB code no handwritten solution required. Write a MATLAB script to solve the following...
I need MATLAB code no handwritten solution required. Write a MATLAB script to solve the following equations using the Cramer’s rule, and compare your results with MATLAB’s root finding method ( 25 pts. ). 8x1 − 2x2 − 1x3 = 5 − 2x1 + 9x2 − 4x3 − x4 = 2 − x1 − 3x2 + 7x3 − x4 − 2x5 = 1 − 4x2 − 2x3 + 12x4 − 5x5 = 1 − 7x3 − 3x4 − 15x5 =...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",  ...
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",   b. extracts the columns of the data from "XYZ" into arrays "c1", "c2", ..., "cN",   c. plots some column versus another column, with a title, and labeled axes.   To test and debug your code, create a "file.dat" with 3 rows and N=2 columns ( so a 3×2 matrix ).   The first column should be sorted. You can just enter some numbers in "file.dat" in...
Name the script for.sh.  This script will create a shopping list. Ask the user to enter items...
Name the script for.sh.  This script will create a shopping list. Ask the user to enter items separated by a space. Read the list. Use a for loop to write (use echo) the items to a file called shopping_list. You should use >> to append the output to the file, so each time the script is run the list should get longer. After the for loop finishes, display (use cat) the contents of the shopping list with 1 item per line....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT