How to plot two piecewise functions on same graph?
Afficher commentaires plus anciens
I need to plot the attached functions on same plot. Please help me to write the Matlab code.
Thanks in advance!
Réponses (1)
Walter Roberson
le 23 Juil 2022
range = [-2 2];
fplot([f, g] , range)
17 commentaires
Amna Habib
le 23 Juil 2022
Amna Habib
le 23 Juil 2022
Walter Roberson
le 23 Juil 2022
Your question defines symbolic formulas, so you need to use the symbolic toolbox or you need to modify the question.
x = linspace(0, 1 ).';
f = @(x) (x<0.5) .* (30.*x) + (x>=0.5).* (70.*x)-20;
g = @(x) (x<0.5).* 30.*(1-x) + (x>=0.5).* 50-(70.*x) ;
figure
plot(x, [f(x), g(x)], 'linewidth', 1.5 )
Amna Habib
le 23 Juil 2022
Amna Habib
le 23 Juil 2022
Walter Roberson
le 23 Juil 2022
'ln'(2-2.*x)
if that was valid syntax at all, then it would mean that you want to take the vector of characters ['l' 'n'] and index that vector at the indices calculated by 2-2.*x, getting back a vector of characters.
By the way, matlab uses log() not ln()
Sam Chak
le 23 Juil 2022
Walter Roberson
le 23 Juil 2022
In Maple you could in theory use code such as
`sqrt`(x)
Everything inside the back quotes becomes part of an atomic name that can be used as an identifier, and there are ways to code symbols and unicode characters. So you could, for example, create a function named `2π`
Commonly, Maple strips the back quotes out in presentation mode (2d output) and renders the symbols, but there are some cases such as copy and paste in 1d (code) mode where it leaves the back quotes unless the characters involved form a valid identifier.
Sam Chak
le 23 Juil 2022
@Walter Roberson, thanks for the background information. 👍
Amna Habib
le 24 Juil 2022
Amna Habib
le 26 Juil 2022
Modifié(e) : Walter Roberson
le 26 Juil 2022
Amna Habib
le 26 Juil 2022
Modifié(e) : Amna Habib
le 26 Juil 2022
You should recheck your definition of g, as it is everywhere complex. Consider for example x = 0, then 2-2*x is 2-0, log(2) is positive, -1.*log(2) is negative, sqrt(-log(2)) is complex.
x = linspace(0, 1 );
f = @(x) (x<0.5) .* (7-3.* sqrt(-2.* (log(2.*x)))) + (x>=0.5).*(7+2.* sqrt(-2.* (log(2-(2.*x)))));
g = @(x) (x<0.5).* (7-5.* sqrt(-1.* (log(2-(2.*x))))) + (x>=0.5).*(7+4.* sqrt(-1.* (log(2.*x))));
figure
plot(x, [f(x); g(x)], 'linewidth', 1.5 )
syms X real
F(X) = piecewise( (X<0.5), (7-3.* sqrt(-2.* (log(2.*X)))), (X>=0.5), (7+2.* sqrt(-2.* (log(2-(2.*X))))), 0)
G(X) = piecewise( (X<0.5), (7-5.* sqrt(-1.* (log(2-(2.*X))))), (X>=0.5), (7+4.* sqrt(-1.* (log(2.*X)))), 0)
limit(F, X, 0)
limit(F, X, 1)
limit(G, X, 0)
limit(G, X, 1)
x = linspace(0, 1 ).';
f = @(x) (x<0.5) .* (30.*x) + (x>=0.5).* (70.*x)-20 ;
g = @(x) (x<0.5).* 30.*(1-x) + (x>=0.5).* 50-(70.*x ) ;
figure
plot(x, [f(x), g(x)], 'linewidth', 1.5 )
syms X real
F(X) = piecewise((X<0.5), (30.*X), (X>=0.5), (70.*X)-20, 0 )
G(X) = piecewise((X<0.5), 30.*(1-X), (X>=0.5), 50-(70.*X), 0)
fplot([F, G], [0 1])
Amna Habib
le 27 Juil 2022
Walter Roberson
le 27 Juil 2022
Look more closely at your functions
f = @(x) (x<0.5) .* (30.*x) + (x>=0.5).* (70.*x)-20 ;
g = @(x) (x<0.5).* 30.*(1-x) + (x>=0.5).* 50-(70.*x ) ;
Notice that the -20 in f not being multiplied by any condition. Notice that the -70.*x in g is not being multiplied by any condition.
Catégories
En savoir plus sur Assumptions dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!












