Question

In: Computer Science

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

Solutions

Expert Solution

x = [-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13]; % take the input array

function f = piecewise(x) %define function - piecewise

for c = 1:17 %Travel through the input array by taking each element where c is index

if x(c) < 0 % x(c) is element of the input array where "c" is index
  
f(c) = -x(c)^3 - 2*x(c)^2 + 3*x(c); %calculate the value as given
  
elseif x(c) <= 8 % x(c) is element of the input array where "c" is index
  
f(c) = (12/pi) * sin(pi*x(c)/4);%calculate the value as given
  
else
  
f(c) = (600*exp(x(c)-8))/(7*(14 + 6*exp(x(c)-8))) -30/7;%calculate the value as given
  
end %end of the if condition
  
end %end of the for loop
  
end %end of the function
  
f = piecewise(x); % call the piecewise function

plot(x,f) %plot graph


Related Solutions

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....
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.
Create a matlab function that converts Miles per hour to feet per second. Please show code...
Create a matlab function that converts Miles per hour to feet per second. Please show code in matlab
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a...
MATLAB Question Write a script to plot the function 'x times sine-squared of x' over a user-specified interval. It should prompt the user to enter the starting and stopping values of x, perform the calculation, and plot the result. Test the script using start/stop values 28 and 42.
using Matlab only please. function sail_boat x = linspace(0,100,2*pi); for x plot(x, sin(x),'b','LineWidth',3) axis([0,2*pi+.1,-1.05,1.05]) hold on...
using Matlab only please. function sail_boat x = linspace(0,100,2*pi); for x plot(x, sin(x),'b','LineWidth',3) axis([0,2*pi+.1,-1.05,1.05]) hold on boat(x,sin(x)) pause(.05) end function boat(x,y) xc = [x-.1,x+.1,x+.2,x-.2,x-.1]; yc = [y,y,y+.1,y+.1,y]; plot(xc,yc,'k') The function is supposed to plot a sine curve and an animation of a representation of a boat moving on top of the curve, but the code has several errors. Notice that the program uses modulization and it calls another function inside sail_boat. Correct the code and submit it with a new...
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...
Matlab Consider f(x) = x^3 - x. Plot it. (a) Use Newton's method with p_0 =1.5...
Matlab Consider f(x) = x^3 - x. Plot it. (a) Use Newton's method with p_0 =1.5 to find an approximation to a root with tolerance TOL = 0.01. (b) Use secant method with p_0 = 0.5 , p_1 =1.5, to answer same question as (a). (c) Use Method of False position with initial approximate roots as in (b).
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!
(only matlab code please) solve and plot the relation between the position vs time where m1=...
(only matlab code please) solve and plot the relation between the position vs time where m1= 1 kg , m2 = 1.5 kg (Mass) k1= 100 N/m , k2 = 125 N/m (spring) b = 1 (damper) Initial conditions: y1(0) =0.15 m , y ’1(0) =0.05 m/s y2(0) =0.05 m , y ’2(0) =0.03 m/s U1(t) = unit step starting at t (step time) = 0 sec U2 = u (t,5) (t (step time) = 5 sec) t = linspace(0,25)...
use a matlab built-in function to numerically solve: dy/dx= -x^2+((x^3*e^-y)/4) for 1<=x<=5 with y(1)=1 plot the...
use a matlab built-in function to numerically solve: dy/dx= -x^2+((x^3*e^-y)/4) for 1<=x<=5 with y(1)=1 plot the solution
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT