Azzera filtri
Azzera filtri

How to plot a linear piecewise function on matlab?

3 visualizzazioni (ultimi 30 giorni)
Amna Habib
Amna Habib il 26 Giu 2023
Commentato: Amna Habib il 26 Giu 2023
Can you please mark the error in the following code?
I'm facing the error:
>> linear
Linear Function:
Parameters: [1x1 struct]
X = 0:0.001:50 ;
f = double(X<20).* (0) + ...
double(and(X>=20,X<35)).* ((X-20)./15) + ...
double(X>=35).*(1);
g = double(X<20).* (1) + ...
double(and(X>=20,X<35)).* ((35-(X))./(15)) + ...
double(X>=35).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
xticks(2:1:10);
  3 Commenti
Dyuman Joshi
Dyuman Joshi il 26 Giu 2023
The code expect for the first line you mentioned runs without any error. I am not sure what the command linear supposed to do, nor do we have the data of linear to run to reproduce the error.
If you are getting an error, copy and paste the full error, which means all of the red text.
Also, there's no need of using double().
X = 0:0.001:50 ;
f = (X<20).* (0) + (and(X>=20,X<35)).* ((X-20)./15) + (X>=35).*(1);
g = (X<20).* (1) + (and(X>=20,X<35)).* ((35-(X))./(15)) + (X>=35).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
%xticks(2:1:10);
DGM
DGM il 26 Giu 2023
... or just
% all you need to define a polyline are the vertices
x = [0 20 35 50];
f = [0 0 1 1];
g = [1 1 0 0];
plot(x,f,'r','linewidth',2);
hold on ;
plot(x,g,'b','linewidth',2);
xlabel('X');

Accedi per commentare.

Risposte (1)

Sam Chak
Sam Chak il 26 Giu 2023
Might as well...
but this approach requires the fuzzy logic toolbox.
x = 0:0.1:50;
f = linsmf(x, [20 35]); % s-shaped function
g = linzmf(x, [20 35]); % z-shaped function
plot(x, [g; f]', 'linewidth', 2), grid on
xlabel('X');

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by