Question

In: Computer Science

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 example, blue squares, cyan stars, red plus signs.  

Use a FOR loop with IF statements to fill in the x vector.  Do not define x as x=-20:1:20.

Solutions

Expert Solution

Code:

k = 1;
for j=-20:20
    x(k) = j;
    k = k+1;
end

for i=1:numel(x)
  if x(i) < 0
    y(i) = (x(i))^2 + 6*x(i) + 8;
  elseif (x(i) >= 0) && (x(i) < 9)
    y(i) = 5*x(i) + 5;
  elseif x(i) >= 9
    y(i) = 4*x(i) + 10;
  end
end

% extracting piecewise values of x and y to plot with different markers
x1 = x(1:20); y1 = y(1:20);
x2 = x(21:29); y2 = y(21:29);
x3 = x(30:41); y3 = y(30:41);

plot(x1,y1,'-s','MarkerSize',10,...
    'MarkerEdgeColor', 'blue')
hold on
plot(x2,y2,'-*','MarkerSize',10,...
    'MarkerEdgeColor', 'cyan')
plot(x3,y3,'-+','MarkerSize',10,...
    'MarkerEdgeColor', 'red')
hold off

(Feel free to throw an upvote or comment below if you have any doubts)


Related Solutions

(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
Write a Matlab program to create specialized plot animation with 16 frames by using fast Fourier...
Write a Matlab program to create specialized plot animation with 16 frames by using fast Fourier transforms of complex matrices
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....
Plot the original data and the regression “line” ************USING Matlab************. "Submit plot" USING MATLAB! USING MATLAB!...
Plot the original data and the regression “line” ************USING Matlab************. "Submit plot" USING MATLAB! USING MATLAB! USING MATLAB! ONLY BY USING MATLAB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 14.9 The concentration of E. coli bacteria in a swimming area is monitored after a storm: t (hr)                     4           8         12        16    20 24 c (CFU/100 mL) 1600     1320   1000     890 650 560 The time is measured in hours following the end of the storm and the unit CFU is a .colony forming unit.. Use this data to estimate (a)...
please Use Matlab to plot simulated results and to plot and conduct the mathematical analysis of...
please Use Matlab to plot simulated results and to plot and conduct the mathematical analysis of the data below: note: I only need these things 1- simulate the RC filter in the frequency domain in order to obtain magnitude and phase response plots. 2-obtain the transient response plots of the capacitor voltage and the current as functions of time. Channel 2 Data Time Scale: 0.002 seconds/div Voltage Scale: 2.0 volts/div Waveform Data: 500 points Time Voltage --------------------- -0.01 2.68675 -0.00996...
(Optics) Write a program in Matlab that can plot the amplitude and energy reflection and transmission...
(Optics) Write a program in Matlab that can plot the amplitude and energy reflection and transmission coefficients for both polarizations as functions of incidence angle for any incidence dielectric medium into any other dielectric medium.
Using matlab a) Find the transfer function of a fourth order Butterworth filter and plot the...
Using matlab a) Find the transfer function of a fourth order Butterworth filter and plot the magnitude response. b) Find the poles.
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
Use MATLAB to plot the likelihood function for estimating the probability of a coin landing heads-up...
Use MATLAB to plot the likelihood function for estimating the probability of a coin landing heads-up without prior knowledge after observing HTHT. -would greatly appreciate if you went in detail on how to find the likelihood function based off the given information.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT