Question

Problem 1 (10 pts) (Matlab coding) In this problem you will be manipulating a sine wave...

Problem 1 (10 pts) (Matlab coding)

In this problem you will be manipulating a sine wave plot using a for loop and if statements.

  1. a) Create a vector x that goes from −4π to 4π with increments of π/10.

  2. b) Instead of using y = sin(x) on the entire array at once, use a for loop to calculate the sine of x for each value in the vector individually. Vector y should be the sine of vector x. Use the following formula as a hint on how to set up your for loop, where yi is the ith element of y and xi is the ith element of x:

    yi = sin(xi)

  3. c) Now, we will be manipulating the sine wave. We want to take all of the negative values of the sine wave and make them positive. You should use an if statement inside your for loop to re-assign the negative y-values to be equal to the corresponding positive value of y. To do this, you can use the following formula inside your if statement:

    yi = abs(yi)

    Only use abs() on yi if yi is negative.

  4. d) Next, we will be clipping the sine wave to stay between 0 and 0.8 on the y-axis. To do this, you should use another if statement inside your for loop to take any y-values greater than 0.8 and set them equal to 0.8.

  5. e) Outside of your for loop, plot sin(x) vs. x. Now, plot y vs. x (which is the manipulated sine wave) on the same plot. Your plot should look like the one below. Add axis-labels and a legend to match the plot below.

Problem 2 (10 pts)

In this this problem you will be using for loops to take a matrix of random number and create a new matrix that is the same as the random matrix, but with zeros on the diagonal.

  1. a) Create matrix m that is a 10x10 matrix of random whole numbers from 1 to 20. You will have to manipulate the random number generator function to modify the interval and get only whole numbers.

  2. b) Create a for loop that runs for index i equal to the index for every row in the matrix. Next, create a nested for loop that runs for index j equal to the index for every column in the matrix. This is very similar to the Your Turn - Extra slide of Lecture 13.

  3. c) Now, we want to use our for loops to create a new matrix, n, that is equal to matrix m, but with zeros on the diagonal. You should use an if/else statement to create this new matrix. If the location is on the diagonal (i=j), then the value of matrix n at the location will be zero. Otherwise, the value of matrix n equals the value of matrix m at each location.

  4. d) Display matrices m and n on separate lines using the disp() function. You should get something like this in your Command Window. The numbers you generate will not be the same, but this is the basic format:

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

First Part-

Code

x = -4*pi:pi/10:4*pi;

y = [];

for i = x
   yi = sin(i);
   if (yi < 0)
       yi = abs(yi);
   endif
   if (yi > 0.8)
       yi = 0.8;
   endif
   y = [y yi];
endfor

figure;
plot(x, sin(x));
hold on;
plot(x, y);
legend("sin(x)", "y");
xlabel("x");
ylabel("sin(x)");

OUTPUT-

Second Part-

code-

x = randi([1 20],10);

y = zeros(size(x));

for i = 1:size(x, 1)
   for j = 1:size(x, 2)
       if (i == j)
           y(i, j) = 0;
       else
           y(i, j) = x(i, j);
endif
endfor
endfor
disp("x-");
disp(x);
disp("y-");
disp(y);

OUTPUT-

I hope it helps. For any doubt, feel free to ask in comments, and give upvote if u get the answer.

Add a comment
Know the answer?
Add Answer to:
Problem 1 (10 pts) (Matlab coding) In this problem you will be manipulating a sine wave...
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
  • how would i do this code in MATLAB Step 2: Coding You are given a vector...

    how would i do this code in MATLAB Step 2: Coding You are given a vector t. We define x = di cos(it) 16 Yi = 1:1.sin(i-1) for i = 1,2,...,15,16 where multiplication of vectors is assumed to be point-wise. Create a 4x4 subplot. - In the ith location, plot the values of x; versus y; with a solid line (pick a color). - Make the axis tight Run the code a few times before submitting. Note that the size...

  • In matlab, I keep getting the same issue for this problem can someone help? "myrowproduct.m" >> Filename...

    In matlab, I keep getting the same issue for this problem can someone help? "myrowproduct.m" >> Filename myrowproduct (A,x) Undefined function or variable 'Filename' Did you mean: mfilename : myrowproduct (A, x) Undefined function or variable 'myrowproduct' function y = myrowproduct(A,x); function y my rowp roduct (A, x); Error: Function definition not supported in this context. Create functions in code file. (, 2,,)T 4. The product y Ax of an m x n matrix A times a vector x= can...

  • Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () func...

    Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () function, create a script that generates a row vector (1 row X 20 columns) of random numbers, each with magnitude between 0 and 5 b) (3 pts) Use the round() function to turn all of the values into whole numbers by rounding down (truncate the decimal part on your vector) c) (5 pts) Add code to your script to generate a new vector...

  • Problem 3 In this problem you add some random noise to the sine function and then...

    Problem 3 In this problem you add some random noise to the sine function and then calculate the derivative. • First, generate a vector (the same length as x, above) which consists of random numbers in the range (-0.02) to (0.02). Use the MATLAB function rand. rand will return a random number between 0 and 1. For this problem, you actually need random numbers between +/- 0.02. How will you convert the random numbers you obtain from rand to be...

  • 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...

  • on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A,...

    on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A, of matrix A is accesser (3) Correcting an entry is easy to (4) Any submatrix of Ais obtained by d row wise. Rows are separated by semicolons and columns are separated by spaces ner A l 23:45 6. B and hit the return/enter kry matrix A is accessed as A Enter and hit the returnerter key an entry is easy through indesine Enter 19...

  • This uses MATLAB code with plotting HW19: Plotting with plot () Due 11:59 pm Monday 1....

    This uses MATLAB code with plotting HW19: Plotting with plot () Due 11:59 pm Monday 1. (10 pts) Write a script that will produce a sine curve and a cosine curve in the same plot, 0 as shown to the right. 06 0.4 0.2 To have two sets of data in one plot, you may plot the first one, then with the command hold on", plot the other. Or, you can learn how to plot two lines with just one...

  • Use Matlab Given: x = [13, 5, 7, 9, 1, 4, 7, 10, 13] Use one...

    Use Matlab Given: x = [13, 5, 7, 9, 1, 4, 7, 10, 13] Use one 'for loop' to create the matrix y of dimension 5X2, using the index to program the address and the value of each element of y; and y = x' Given, x = [2, 3, 4, 5, 3, 4, 5, 6] Use two 'for loops' to create the matrix of dimension 4X2 Use a while statement to determine when the sum of the numbers in...

  • can you write a matlab code PROJECT DELIVERABLE 1: In Project Deliverable 1, you will write...

    can you write a matlab code PROJECT DELIVERABLE 1: In Project Deliverable 1, you will write a Matlab code to automatically run your Simulink model where two parameters change, and save the performance index. Finally, youu plot the corresponding performance index as a 3D plot. Step 1) Build the following system in Simulink and name it as "FirstOrderSystem_ProjectGroupnumber.mdl". Finrderte Diegram S dion oulo Ro Cede Helps Md re Prrd K sebr Stro Transter Ecn gdet 100% Ready In this system,...

  • MATLAB QUESTION !!! PLEASE USE MATLAB FOR BOTH QUESTIONS!!! THANKS 3) Below is a matrix of...

    MATLAB QUESTION !!! PLEASE USE MATLAB FOR BOTH QUESTIONS!!! THANKS 3) Below is a matrix of random numbers. Find the mean, median, and mode of the matrix. B=[0 1 2 3 4 50689 23092 6 8 407] Sort Matrix Z so that the largest numbers of each row are in the first column and smallest numbers descend towards the last column. Must use sort function. 4) Create a matrix X so that the first row has values from 0 to...

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