Question

MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n

so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can.

BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN USE IT TO COME UP WITH A SCRIPT THAT DOES USE AN N INPUT (like n = ( 'input n ');)

P4.1.11 Write a script that inputs a positive integer n and then generates a length-10 row vector f according to the following formula: 1n ifk=1 k-1 fk-1/2 if 10 k1 and fk- is even. Your seript should plot the points (1.f10, fio) using the star marker

I have named the function as fk.

The following code has to be copied into a file named fk.m

function y = fk(k)
if (k == 1)
y = 1; % ACCORDING TO THE PROBLEM, THIS SHOULD PRINT n, NOT 1.
elseif(k > 1 && k <= 10)
previous = fk(k-1); % get fk(k-1), i.e. value of fk for k-1
if(mod(previous, 2) == 1) %if fk-1 is odd
y = 3 * previous + 1;
else %fk-1 is even
y = previous / 2;
end
else
y = 0;
end
end

============================

Now in a different file you copy this code

x = 1:10;
y = []; %start with an empty vector

%iterate over x value
for k = 1:10
y = [y , fk(k)] ; %append the value of function to vector
end

%now use x and y values and star marker to plot the graph
plot(x, y, '*')

The above code will produce the following output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below code satisfies the given requirements as follows:

--------------------------------------------------------------------------

function y = fk(n) %%creating a function named fk which takes input as n and output a vector y

k=1 %%Initializing k value with 1

while (k >= 1 && k <= 10) %%while loop for k 10 times

if (k==1) %% if k is 1 y will be n

y(1)=n

else

if (mod(previous, 2) == 1) %%if the fk(k-1) is odd

y(k) = (3 * previous) + 1

elseif (mod(previous, 2) ~= 1) %%if the fk(k-1) is even

y(k) = previous / 2

else

y=0

end

end

k=k+1 %%increasing k value with 1

previous = y(k-1) %%storing the previous value into variable previous

end

end

For example, I am initializing the n value with 2 and plotting the required values to the graph with * as follows:

n=2

y = fk(n)

x = 1:10;

plot(x, y,'*')

xlabel("X-Axis")

ylabel("Y-Axis")

Screenshot of Output and code as in Matlab Edditor:-

Add a comment
Know the answer?
Add Answer to:
MATLAB Question: Write a script for the problem pictured. Please send the script, not just the...
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
  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to...

    Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to symbolically compute the derivative of ln(x) on the interval from -5 to 5, containing 100 data points. Your script file should output a plot of the derivative of ln(x) (taken from your function) vs x. Use a line with circular markers for the plot and include a grid and appropriate labels. The user-defined function should 1. Receive a single x value as input. 2....

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a =...

    Only calculate manually 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a = 0.1. Plot the value of x(t). Since the e-af almost attenuates to 0 after 5T, where T1. So make your time vector from 0 to 5T would be enough to capture the signal.) 2. Using the definition of the FT (1), manually calculate the FT of the given functions in previous exercise 0O x(t)e 3. Plot the absolute values and...

  • Please follow following scripts: Script for part A:Script for Part B: Matlab Exercises 1. Find the...

    Please follow following scripts: Script for part A:Script for Part B: Matlab Exercises 1. Find the determinant (command: det(A)) and the condition number (command: cond(A)) of the Hilbert matrix H of order k (command: hilb(k)), for k = 1,2, ..., 10. Plot the determinant and the condition number as a function of k using a logarithmic scale for the vertical axis. Hint: The template to help you with this homework assignment is homework/hw15.m. 2. Determine if y is in the...

  • using matlab 3. Write a script that will read the ages vector.dat file created in the...

    using matlab 3. Write a script that will read the ages vector.dat file created in the previous problem. - the script will print the number of persons in the group - the script will print the maximum and minimum ages in the group - the script will compute and print the "average age" The script should return something like: The file contains the ages of a group of 50 people The maximum and minimum ages in the group are 10...

  • MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the...

    MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the following math function 3 o-0.47x The input to the function is x and the output is y. Write the function such that x can be an array (use element-by-element operations) (15 pts) Use the function in (a) the command window to calculate y(-2) and y(5) (b) a script file HW7P2.m to determine y(x) for 0.001 Sx S 10 with 1000 points. Hint: Use the...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

  • Problem 7. [MAILABⓒproblem] Write a short MATLAB script to construct the transfer function of a system...

    Problem 7. [MAILABⓒproblem] Write a short MATLAB script to construct the transfer function of a system that is described by the following poles, zeros, and gain zeros =-1,1 ±2j poles =-2土2,-0.4 k = 1.28 and plot its response to a step input with amplitude 5 (meaning, u(t)-5 × 1(t). Determine the system's (1) time constant and (2) rise time from the plot of the step response. (Submit the MATLABO script and the plot; both should fit into one page. You...

  • Matlab problem using newton raphson to find square root of number

    Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.-I need to create these functions:A function to find f(x)A function to find f '(x) ?A...

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
ADVERTISEMENT