Plot functions in MATLAB
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
Hello every one,
If possible..I need to draw these five functions (in the picture) at the same time so that the output is one drawing containing the drawing of all these five functions with one different color for each function..please, is this possible in MATLAB?
And put the square of the indicative color for each function on the output drawing.
This is my attempt, but I got an error while executing
If any Prof. can help me...thanks alots

採用された回答
Les Beckham
2021 年 8 月 10 日
Since I don't have the Statistics and Machine Learning Toolbox (for the unifrnd() function), I used the base Matlab function rand() instead.
% v1=unifrnd(0,1,1,m);
% l1=unifrnd(0,1,1,m);
% u1=unifrnd(1,2,1,m);
v1 = rand(1,m);
l1 = rand(1,m);
u1 = rand(1,m)*2;
That is the only change I made and the script generated a plot.
When you post a question to Matlab Answers, I would suggest showing the exact error message that you are seeing, or explaining how your result differs from what you expect. Please show your error message (all of the red text that comes up in the command window).
Your code is generating 100 different lines in the plot (since G is 100x101) rather than the 5 that you suggest that you expect.
11 件のコメント

Thanks alot for your reply ...and thanks for your suggestion.. that is a result in my matlab.
yes my expected code is generating 100 different lines for each function,
Did the drawing appear to you?please
Can you share it with the full code , so that I know my mistake?
Is it possible to use a different color for each function? please , such as
any one function consisting of 100 diagrams is red in color, while the other functions are in different colors...Is that possible?

The drawing does not appear for me
Les Beckham
2021 年 8 月 11 日
This is the plot. I'm not sure what you were expecting.

This is the code.
format long
m=100;
% v1=unifrnd(0,1,1,m);
% l1=unifrnd(0,1,1,m);
% u1=unifrnd(1,2,1,m);
v1 = rand(1,m);
l1 = rand(1,m);
u1 = rand(1,m)*2;
a = 0.1*l1';
b = 2*v1';
c = 3*v1';
d = 4*u1';
e=5*v1';
f=6*u1';
x = 0:0.1:10;
%g1%%%%%
part0 = 0 .* ( x >= b);
part1 = (5-x)./(b-a) .* (x >= a & x <= b);
part2 = 2 .* ( x <= a);
%g2%%%%%
part3 = 0 .* (x<=a);
part4= (x-4)./(b-a) .* (x >= a & x <= b);
part5 = (6-x)./(c-b) .* (x >= b & x <= c);
part6 = 0 .* (x >= c);
%g3
part7 = 0 .* (x<=b);
part8= (x-5)./(b) .* (x >= b & x <= c);
part9= 2 .* (x >= c & x <= d);
part10= (8-x)./(e-d) .* (x >= d & x <= e);
part11= 0 .* (x >= e);
%g4
part12 = 0 .* (x<=d);
part13= (x-7)./(e-d) .* (x >= d & x <= e);
part14= (9-x)./(6) .* (x >= e & x <= f);
part15= 0 .* (x >= f);
%g5
part16 = 0 .* ( x <= e);
part17 = (x-8)./(15) .* (x >= e & x <= f);
part18 = 2 .* (x >= f );
G = part0 + part1 + part2 + part3 + part4 + part5 + part6+ part7+ part8+ part9+part10+part11+part12+part13+part14+part15+part16+part17+part18;
plot(x, G);ylim([-0.1 1.1*max(G(:))])
legend
thanks alot prof.
I could not write the code in the right form, the functions g1 , and g2, and g3,and g4,and g5 ,did not appear clearly ,,,I could not distinguish g1 , and g2, and g3,and g4,and g5
I mean , I want to plot g1 (with all lines)in one color, then plot g2 (with all lines)in another color ,...to g5(with all lines) in a different color , the output colour are 5 color only ,
...such as
1- the first color for g1 which is
part0 = 0 .* ( x >= b);
part1 = (5-x)./(b-a) .* (x >= a & x <= b);
part2 = 2 .* ( x <= a);
g1 = part0 + part1 + part2;
plot(x, g1);;ylim([color 1, as red])
2- the second color for g2 which is
part3 = 0 .* (x<=a);
part4= (x-4)./(b-a) .* (x >= a & x <= b);
part5 = (6-x)./(c-b) .* (x >= b & x <= c);
part6 = 0 .* (x >= c);
g2 = part3 + part4 + part5 + part6;
plot(x, g2);;ylim([color 2, as blue])
2- the third color for g3 which is
part7 = 0 .* (x<=b);
part8= (x-5)./(b) .* (x >= b & x <= c);
part9= 2 .* (x >= c & x <= d);
part10= (8-x)./(e-d) .* (x >= d & x <= e);
part11= 0 .* (x >= e);
g3 = part7+ part8+ part9+part10+part11;
plot(x, g3);;ylim([color 3, as green])
4- the 4 color for g4 which is
part12 = 0 .* (x<=d);
part13= (x-7)./(e-d) .* (x >= d & x <= e);
part14= (9-x)./(6) .* (x >= e & x <= f);
part15= 0 .* (x >= f);
g4 = part12+part13+part14+part15;
plot(x, g4), ;ylim([color 4, as Orange])
5- the 5 color for g5 which is
part16 = 0 .* ( x <= e);
part17 = (x-8)./(15) .* (x >= e & x <= f);
part18 = 2 .* (x >= f );
g5 = part16+part17+part18;
plot(x, g5);ylim([color 5 as yellow])
....................
the square of the indicative color in the output for g1, g2,g3,g4,g5
...........................
This is another attempt, can you modify it or give me the appropriate code?thanks alot
Les Beckham
2021 年 8 月 14 日
I adapted your second attempt and now we are getting 500 lines on the plot. Again, because your g1-g5 variables have dimensions 100x101. Since I don't understand your math or what you are attempting to model here, I don't know what to suggest.
Here is the updated code. I used cyan for g4 instead of orange as it is easier to specify with the "short name" form of a color specifier.
format long
m=100;
% v1=unifrnd(0,1,1,m);
% l1=unifrnd(0,1,1,m);
% u1=unifrnd(1,2,1,m);
v1 = rand(1,m);
l1 = rand(1,m);
u1 = rand(1,m)*2;
a = 0.1*l1';
b = 2*v1';
c = 3*v1';
d = 4*u1';
e=5*v1';
f=6*u1';
x = 0:0.1:10;
%% Attempt #2
% 1- the first color for g1 which is
part0 = 0 .* ( x >= b);
part1 = (5-x)./(b-a) .* (x >= a & x <= b);
part2 = 2 .* ( x <= a);
g1 = part0 + part1 + part2;
% plot(x, g1);;ylim([color 1, as red])
% 2- the second color for g2 which is
part3 = 0 .* (x<=a);
part4= (x-4)./(b-a) .* (x >= a & x <= b);
part5 = (6-x)./(c-b) .* (x >= b & x <= c);
part6 = 0 .* (x >= c);
g2 = part3 + part4 + part5 + part6;
% plot(x, g2);;ylim([color 2, as blue])
% 3- the third color for g3 which is
part7 = 0 .* (x<=b);
part8= (x-5)./(b) .* (x >= b & x <= c);
part9= 2 .* (x >= c & x <= d);
part10= (8-x)./(e-d) .* (x >= d & x <= e);
part11= 0 .* (x >= e);
g3 = part7+ part8+ part9+part10+part11;
% plot(x, g3);;ylim([color 3, as green])
% 4- the 4 color for g4 which is
part12 = 0 .* (x<=d);
part13= (x-7)./(e-d) .* (x >= d & x <= e);
part14= (9-x)./(6) .* (x >= e & x <= f);
part15= 0 .* (x >= f);
g4 = part12+part13+part14+part15;
% plot(x, g4), ;ylim([color 4, as Orange])
% 5- the 5 color for g5 which is
part16 = 0 .* ( x <= e);
part17 = (x-8)./(15) .* (x >= e & x <= f);
part18 = 2 .* (x >= f );
g5 = part16+part17+part18;
% plot(x, g5);ylim([color 5 as yellow])
plot(x, g1, 'r-s', x, g2, 'b-s', x, g3, 'g-s', x, g4, 'c-s', x, g5, 'y-s')
And here is the plot

thank you very very much prof. Les
It is required as an assignment for me how to draw this type of functions and I do not know what functions will come in the exam
Any suggestion or idea that comes to your mind might benefit me . Thank you for your help,
if possible , please ...I need the square of the indicative color for each g1,g2,g3,g4,g5 , not for data
Les Beckham
2021 年 8 月 14 日
I think you need to ask your instructor for an explanation of the math behind the creation of these functions as it doesn't make much sense to me. Since you have no comments in your code explaining what each of the inputs and outputs are, nor any explanation of the operations you are performing to convert the inputs into the outputs, I don't think I'm going to be able to help much more.
Can you explain what you mean by "I need the square of the indicative color for each g1,g2,g3,g4,g5 , not for data".
I assumed that you wanted square markers of the desired color on the plot lines (which is what the provided code does). Perhaps I was mistaken.
hasan s
2021 年 8 月 14 日
thanks a lot prof.
I mean I want 

without the following

Les Beckham
2021 年 8 月 14 日
編集済み: Les Beckham
2021 年 8 月 14 日
If it acceptable to have 500 lines and you just want the legend to have five entries, try adding this at the end (in place of any legend command that is there now):
hlines = get(gca, 'children');
legend(flip(hlines([1:100:end])), 'g1', 'g2', 'g3', 'g4', 'g5')
This should be the result:

hasan s
2021 年 8 月 14 日
This is exactly what I want, thank you very much prof. Les
Les Beckham
2021 年 8 月 15 日
Glad I could help.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
タグ
参考
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)
