Write a MATLAB code where I'll show the plot or graph.

조회 수: 4 (최근 30일)
AZ Sajjad
AZ Sajjad 2023년 9월 10일
댓글: Walter Roberson 2023년 9월 10일
Here is the condition. Now write a MATLAB code where I'll show the plot or graph.
y(x) = 3.75x+10 at 0<x<2
y(x) = -2.75x+22.5 at 2<x<4
y(x) = 12.5 at 4<x<6
y(x) = 2.5x-2.5 at 6<x<8
The plot or graph will be like this.

답변 (2개)

Walter Roberson
Walter Roberson 2023년 9월 10일
x = rand() * 2
x = 0.0101
y = @(t) -42*x + 53;
T = linspace(-3, 3);
for idx = 1 : length(T)
Y(idx) = y(T(idx));
end
plot(T, Y)
ylim([-90 90])
Why? Well, you define y as functions of t but there is no t in the formulas, so we have to assume that the items such as 0 < x < 2 mean that x is to some one random element of the given range.
  댓글 수: 2
AZ Sajjad
AZ Sajjad 2023년 9월 10일
I edited my question. Now would you help me out?
Walter Roberson
Walter Roberson 2023년 9월 10일
I would point out that I illustrated several important points:
  • constructing anonymous functions
  • defining a range of values to evaluate over
  • evaluating a non-vectorized function over a non-integer list of values
  • plotting given a range and associated values
  • adjusting plot y view to make sure the plot is entirely in-frame
The only extra thing you need beyond this is to either use hold on or to plot multiple lines in the same plot() call. Oh, and how to pass color information to plot(). See the plot() documentation.

댓글을 달려면 로그인하십시오.


Sam Chak
Sam Chak 2023년 9월 10일
This is a piecewise linear function because it is defined on a sequence of intervals, and there are a few ways to plot it. However, for beginners, I think that the following approach is easy to understand because you only need to specify the interval and expression for each linear function. I'll show how to plot the first three functions (you will handle the fourth one). Also, please note that the piecewise function contains two jump discontinuities, one at and another at .
If you want the linear functions to be continuous on their corresponding intervals, you will need to modify the equations so that and .
x1 = 0:0.01:2;
y1 = 3.75*x1 + 10; % 0 < x < 2
x2 = 2:0.01:4;
y2 = - 2.75*x2 + 22.5; % 2 < x < 4
x3 = 4:0.01:6;
y3 = 12.5*ones(1, numel(x3)); % 4 < x < 6
plot([x1, x2, x3], [y1, y2, y3])
xlabel('x')
ylabel('y')
title('Piecewise function')
label = {'17.5','17.0','11.5'};
yline([17.5 17.0 11.5], '--', label, 'color', '#A1B2C3')

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by