Question: I am using matlab 1. Write a function called collatz below where you take an input n, and if n is odd, return 3*n+1. If

I am using matlab

1. Write a function called "collatz" below where you take an input n, and if n is odd, return 3*n+1. If n is even, return n/2. (You need to compute mod(n,2) to test for even/odd. If mod(n,2)==1, n is odd, otherwise n is even.) Note: You cannot use the keyword function when you publish. To avoid errors, paste your function code below as comments.

2. Please make sure your collatz function from the previous problem is correct first. Step 1: Set up a list of 100 blank counters (i.e., c=zeros(1,100)) and a list of x values x=1:100. Step 2: Use a while loop inside a for loop to reassign x(i) to the value collatz(x(i)), and every time it reassigns x(i), add 1 to counter c(i). (Hint: Your while loop should run as long as x(i) is not equal to 1, and your for loop should loop over all entries of x). What you are checking is how many times you have to apply the collatz function in order for x to reach 1 given different initial conditions.

3. Plot your list of counters from #3 as a scatter plot of x (the integers from 1 to 100) vs. c. Instead of using the markers MATLAB gave you, please plot the points as red diamonds when you use the scatter or plot commands. Label your vertical and horizontal axes and give the plot a title (These can be whatever you wish).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!