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

5 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

Walter Roberson
Walter Roberson il 10 Set 2023
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 Commenti
AZ Sajjad
AZ Sajjad il 10 Set 2023
I edited my question. Now would you help me out?
Walter Roberson
Walter Roberson il 10 Set 2023
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.

Accedi per commentare.


Sam Chak
Sam Chak il 10 Set 2023
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')

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by