Question

Using for loop and if statement, write a MATLAB code to plot a graph for x(t)...

Using for loop and if statement, write a MATLAB code to plot a graph for x(t) as a function of time t in the range 0 to 12 in increment of 0.01

?(?) =

1: 0 ≤ ? ≤ 1

2? − 1 1 ≤ ? ≤ 2

3 2 ≤ ? ≤ 3

−2.5? + 10.5 3 ≤ ? ≤ 5

−2 5 ≤ ? ≤ 6

4/3 ? − 10 6 ≤ ? ≤ 9

2 9 ≤ ? ≤ 12

Homework Answers

Answer #1

A graph for the given function is plotted in MATLAB in the range 0 to 12 with an increment of 0.01. The graph as well as the MATLAB code are provided below:

MATLAB code:


t=0:0.01:12;
x=0*t;

for i=1:numel(t)
if t(i)>=0 && t(i)<=1
x(i)=1;
elseif t(i)>=1 && t(i)<=2
x(i)=2*t(i)-1;
elseif t(i)>=2 && t(i)<=3
x(i)=3;
elseif t(i)>=3 && t(i)<=5
x(i)=-2.5*t(i)+10.5;
elseif t(i)>=5 && t(i)<=6
x(i)=-2;
elseif t(i)>=6 && t(i)<=9
x(i)=4*t(i)/3-10;
elseif t(i)>=9 && t(i)<=12
x(i)=2;
end
end

plot(t,x);
xlabel('t')
ylabel('x(t)')
title('t vs x(t)')
axis([min(t) max(t) min(x)-.5 max(x)+.5]);

Know the answer?
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
Similar Questions
Write a MATLAB code to plot a contour graph of f(x, y) = x^2 + y^2...
Write a MATLAB code to plot a contour graph of f(x, y) = x^2 + y^2 for −2 ≤ x ≤ 2 and −3 ≤ y ≤ 3. Use the interval of 0.1 in the grid.
Matlab Create plot of the following density functions using x values between -10 and 10 with...
Matlab Create plot of the following density functions using x values between -10 and 10 with an increment of 0.02 -Normal cumulative distribution function with mu=1, sigma=1, mu=0, sigma=2, mu=0,sigma=1/2
Write a Matlab program to plot the cosine wave. That is plot y=cos(k1x) vs. x for...
Write a Matlab program to plot the cosine wave. That is plot y=cos(k1x) vs. x for x varying from 0 to 2pi radians. The value of k1 is given at the end of this document. You can choose the increment for x. Note that large values of the increment will give you a coarse graph. Note: Matlab has two functions to calculate the sine of an angle: sin(x) and sind(x). What is the difference between these two functions? Use help...
Can you rewrite this MATLAB code using a while loop instead of a for loop? %formatting...
Can you rewrite this MATLAB code using a while loop instead of a for loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates...
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates average of positive, AvgPos, and negative, AvgNeg, elements of array X, using the for‐end loop and if‐elseif‐else‐end selection structure. Do not use build‐in MATLAN functions in calculations. Apply the developed function for the following vector X = [ ‐7, 1, 0, 0, 12, 6, 33.2, ‐7.5 ];
Using MatLab 2. Given the parametric equations x = t^3 - 3t, y = t^2-3: (a)...
Using MatLab 2. Given the parametric equations x = t^3 - 3t, y = t^2-3: (a) Find the points where the tangent line is horizontal or vertical (indicate which in a text line) (b) Plot the curve parametrized by these equations to confirm. (c) Note that the curve crosses itself at the origin. Find the equation of both tangent lines. (d) Find the length of the loop in the graph and the area enclosed by the loop. 3. Use what...
Using Matlab, evaluate ? = (? + 10) (?^5 + 1) for values of x in...
Using Matlab, evaluate ? = (? + 10) (?^5 + 1) for values of x in the range of -1 to 1 in step sizes of 0.1. Hence, plot y versus x. Could you also include the Matlab code and the associated screenshots?
Using Matlab to solve the problem below Given X=[-2 -1 0 1 2] Y=[1.5 3.2 4.5...
Using Matlab to solve the problem below Given X=[-2 -1 0 1 2] Y=[1.5 3.2 4.5 3.4 2] a). Plot a scatter plot of the data b). Determine the coefficients of the polynomial ?0 + ?1? + ?2?2 which best fits the data c). Plot this function on the same plot as in part ‘a’. USE MATLAB CODE ONLY! USE MATLAB CODE ONLY! THANK YOU
Fourier Series Approximation Matlab HW1:     You are given a finite function xt={-1 0≤t≤5; 1 5<t≤10...
Fourier Series Approximation Matlab HW1:     You are given a finite function xt={-1 0≤t≤5; 1 5<t≤10 .            Hand calculate the FS coefficients of x(t) by assuming half- range expansion, for each case below. Modify the code below to approximate x(t) by cosine series only (This is even-half range expansion). Modify the below code and plot the approximation showing its steps changing by included number of FS terms in the approximation. Modify the code below to approximate x(t) by sine...
Assignments Generate and plot the signal x1(t) = 1+ sin (4pt), for t ranging from -1...
Assignments Generate and plot the signal x1(t) = 1+ sin (4pt), for t ranging from -1 to 1 in 0.001 increments. Use proper axes labels with title. Generate and plot the function x2(t) = sin (30pt), for t ranging from -1 to 1 in 0.001 increments. Use proper axes labels with title. Generate and plot the combination function x3(t) = x1(t)*x2(t) as above. Use proper axes labels with title. Generate and plot the sum of two cosine waves   v1(t) =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT