Question

In: Computer Science

This code is to be written in Matlab. Write a function that will plot cos(x) for...

This code is to be written in Matlab.

Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values. (e.g., if 3 is passed, the line widths will be 3, 6, and 9.) The line widths will be printed on these plots.

Solutions

Expert Solution

Program screenshot along with output :-

Output

Code in text format :-

function [output] = plotCos(linewidth)

%PlotCos Summary of this function goes here

x = -pi:0.1:pi;

fn = cos(x);

if nargin == 1

l = linewidth;

else

l = 1;

end

lw1 = l * 1;

lw2 = l * 2;

lw3 = l * 3;

figure;

plot(x,fn,'*','LineWidth',lw1);

title(['Linewidth is ' num2str(lw1)]);

figure;

plot(x,fn,'*','LineWidth',lw2);

title(['Linewidth is ' num2str(lw2)]);

figure;

plot(x,fn,'*','LineWidth',lw3);

title(['Linewidth is ' num2str(lw3)]);

end


Related Solutions

Please show MATLAB code to plot below function from x = −3 to x = 12:...
Please show MATLAB code to plot below function from x = −3 to x = 12: function f = piecewise(x) % implements piecewise function using if statements if x < 0     f = -x^3 - 2*x^2 + 3*x; elseif x <= 8     f = (12/pi) * sin(pi*x/4); else     f = (600*exp(x-8))/(7*(14 + 6*exp(x-8))) -30/7; end
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a...
Write a matlab code for given task Use your ‘sin’ or ‘cos’ function to generate a sinusoid wave having two components as f1 = 3kHz and f2 = 5kHz and then sample it with fs = 10kHz. Calculate its fft with zero frequency component in the middle. Plot it on a properly scaled w-axis. Specify if there is aliasing or not? If there is aliasing specify which component is casing the aliasing
Practice for Matlab. You can create own f(x). a. Write code to find and plot a...
Practice for Matlab. You can create own f(x). a. Write code to find and plot a root using the modified secant method b. Find the roots using the roots command - identify and plot root of interest c. Use polyder and roots to find and plot the minimum value d. use fminbnd to find and plot minimum value Thank you!
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program,...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program, and use a while loop, to calculate cos(150) (the input is in degrees) by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Make sure to make the required degree <-> radian conversions. Use fprintf to print the cos(150) (up to 2 decimal places) and the number of terms used to...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to 3π/2 with increments equal to Δx,where Δx and n(the number ofterms used in the series approximation) should be input arguments of the function andshould be specified by the user.
Please use MATLAB. Write a program to plot the piecewise function using IF statements and a...
Please use MATLAB. Write a program to plot the piecewise function using IF statements and a FOR loop in MatLab. Use x as the input vector. Plot from -20<=x<=20, increment of 1.   y =   4 x + 10 ,             ? ≥ 9,                   y =      5? + 5,                   0 ≤ ? < 9,                    y =     ?2 + 6? + 8, ? < 0 Plot each segment of the fn with different shaped points and a different color, for...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Write as a MatLab script For X=-2π~2π with intervals of π/100. a. Plot Y1=sin(X) as a...
Write as a MatLab script For X=-2π~2π with intervals of π/100. a. Plot Y1=sin(X) as a red line. Add a grid. b. Plot Y2=cos(X) as a black dotted line. Add a grid. c. Plot Y1 and Y2 in the same plot without using the hold on/off command. d. For Y3= Y1 times Y2, plot Y1, Y2 and Y3 in the same graph. Y3 will be green line. Add title, axis label and a legend.
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT