Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Matlab code error please help! In execise 2 I declare function geomsum2 but when I was trying to declare p =1 in execise 3 an
Matlab code error please help!
In execise 2 I declare function geomsum2 but when I was trying to declare p =1 in execise 3 an error show up. I still need to declare P=1 so how to solve this error?
Or in other word, ho do I split up those two section, so the function from execise 2 wont affect execise 3? Thanks!
Exercise 2 Part (a) NOTE: We must create separate functioin M-files here. The function will require input variables. Therefore, you cannot use 'run' to execute the function. You must invoke it by providing values for the input variables. Name the function geomsum 1, then complete the follwing. Display contents of geomsum1 M-file 17 type 'geomsum1.m' Assign values to input variables 18 19 r =4/5; a = 4; n=8; 20 21 22 geomsum1(r, a,n) NOTE: Do NOT define ra, or n inside the function file. This defeats the purpose of requiring input arguments. Delete this note before submitting. Compute geometric sum for specified values of ra, and n. 23 24 25 26 27 28 29 30 function sum=geomsum1(a,r,n) sum=a; current=a; for i=1:n current=current*r; sum=sum+current; end end Part (b) NOTE: MATLAB has several built in functions. Here, we want to use the built- Exercise 3 Part (a) NOTE: When the protocol says to write a script file, you do not need to create a separate M-file. This main file is a script file and you can simply write the list of commands here. Delete this note before submission. Initiate product P. P = 1; . Function definitions in a script must appear at the end of the file. Move all statements after the "geomsum2" function definition to before the first local function definition. Define starting iteration index. m = 1; Define stepsize of iteration. k = 2; Define ending iteration index. n = 13; Compute product. for i = m:k:n P = i * P; end % muliply P by next element at each iteration Display product. result NOTE: i = m:k:n is also a vector! A for-loop essentially iterates through each element of a vector. Keep this in mind for part (b). Exercise 2 Part (a) NOTE: We must create separate functioin M-files here. The function will require input variables. Therefore, you cannot use 'run' to execute the function. You must invoke it by providing values for the input variables. Name the function geomsum 1, then complete the follwing. Display contents of geomsum1 M-file 17 type 'geomsum1.m' Assign values to input variables 18 19 r =4/5; a = 4; n=8; 20 21 22 geomsum1(r, a,n) NOTE: Do NOT define ra, or n inside the function file. This defeats the purpose of requiring input arguments. Delete this note before submitting. Compute geometric sum for specified values of ra, and n. 23 24 25 26 27 28 29 30 function sum=geomsum1(a,r,n) sum=a; current=a; for i=1:n current=current*r; sum=sum+current; end end Part (b) NOTE: MATLAB has several built in functions. Here, we want to use the built- Exercise 3 Part (a) NOTE: When the protocol says to write a script file, you do not need to create a separate M-file. This main file is a script file and you can simply write the list of commands here. Delete this note before submission. Initiate product P. P = 1; . Function definitions in a script must appear at the end of the file. Move all statements after the "geomsum2" function definition to before the first local function definition. Define starting iteration index. m = 1; Define stepsize of iteration. k = 2; Define ending iteration index. n = 13; Compute product. for i = m:k:n P = i * P; end % muliply P by next element at each iteration Display product. result NOTE: i = m:k:n is also a vector! A for-loop essentially iterates through each element of a vector. Keep this in mind for part (b)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started