現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to plot two piecewise functions on same graph?
3 ビュー (過去 30 日間)
古いコメントを表示
I need to plot the attached functions on same plot. Please help me to write the Matlab code.
Thanks in advance!
回答 (1 件)
Walter Roberson
2022 年 7 月 23 日
range = [-2 2];
fplot([f, g] , range)
17 件のコメント
Amna Habib
2022 年 7 月 23 日
can you please send me full code?
I have tried but it is not running.
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 )
grid
axis([min(x) max(x) 0 50 ])
xlabel('x ')
ylabel('Function Value ')
legend('f(x)','g(x)', 'Location','best ')
Amna Habib
2022 年 7 月 23 日
The above was some other function. Both are not running.
x = linspace(0, 1 );
f = @(x) (x<0.5) .* (7-3.* 'sqrt'(-2.* ('ln'(2.*x)))) + (x>=0.5).*(7+2.* 'sqrt'(-2.* ('ln'(2-2.*x))));
g = @(x) (x<0.5).* (7-5.* 'sqrt'(-1.* ('ln'(2-2.*x)))) + (x>=0.5).*(7+4.* 'sqrt'(-1.* ('ln'(2.*x))));
figure
plot(x, [f(x); g(x)], 'linewidth', 1.5 )
grid
axis([min(x) max(x) 0 1 ])
xlabel('x ')
ylabel('Function Value ')
legend('f(x)','g(x)', 'Location','best ')
Walter Roberson
2022 年 7 月 23 日
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
2022 年 7 月 23 日
Can you please search the error in this code?
x = linspace(0, 1 );
f = @(x) (x<0.5) .* (7-3.* 'sqrt'(-2.* ('ln'(2.*x)))) + (x>=0.5).*(7+2.* 'sqrt'(-2.* ('ln'(2-2.*x))));
g = @(x) (x<0.5).* (7-5.* 'sqrt'(-1.* ('ln'(2-2.*x)))) + (x>=0.5).*(7+4.* 'sqrt'(-1.* ('ln'(2.*x))));
figure
plot(x, [f(x); g(x)], 'linewidth', 1.5 )
Walter Roberson
2022 年 7 月 23 日
'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()
Walter Roberson
2022 年 7 月 23 日
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.
Amna Habib
2022 年 7 月 26 日
編集済み: Walter Roberson
2022 年 7 月 26 日
i have corrected this code but I think there is still an error in function 'g'. can you please mention?
here is the code. the the file showing graphical result is attached.
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 )
Warning: Imaginary parts of complex X and/or Y arguments ignored.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077895/image.png)
Amna Habib
2022 年 7 月 26 日
編集済み: Amna Habib
2022 年 7 月 26 日
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 )
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077900/image.png)
Sorry I have another confusion in this plot too. I didn't found any error but the graph is not correct as compared to the manual plotting. Here is the code and tghe graph file is attached in .png
Thanks in advance!
Walter Roberson
2022 年 7 月 26 日
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 )
Warning: Imaginary parts of complex X and/or Y arguments ignored.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078255/image.png)
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)
F(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078260/image.png)
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)
G(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078265/image.png)
limit(F, X, 0)
ans = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078270/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078270/image.png)
limit(F, X, 1)
ans =
∞
limit(G, X, 0)
ans = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078275/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078275/image.png)
limit(G, X, 1)
ans = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078280/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078280/image.png)
Walter Roberson
2022 年 7 月 26 日
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 )
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078955/image.png)
syms X real
F(X) = piecewise((X<0.5), (30.*X), (X>=0.5), (70.*X)-20, 0 )
F(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078960/image.png)
G(X) = piecewise((X<0.5), 30.*(1-X), (X>=0.5), 50-(70.*X), 0)
G(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078965/image.png)
fplot([F, G], [0 1])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078970/image.png)
Amna Habib
2022 年 7 月 27 日
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 )
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078930/image.png)
syms X real
F(X) = piecewise((X<0.5), (30.*X), (X>=0.5), (70.*X)-20, 0 )
F(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078935/image.png)
G(X) = piecewise((X<0.5), 30.*(1-X), (X>=0.5), 50-(70.*X), 0)
G(X) =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078940/image.png)
fplot([F, G], [0 1])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1078945/image.png)
Walter Roberson
2022 年 7 月 27 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)