필터 지우기
필터 지우기

need help to plot function contain condition 'equal '

조회 수: 2 (최근 30일)
hasan s
hasan s 2021년 8월 30일
댓글: hasan s 2021년 8월 31일
Hello every one,
I need to plot this function of pentagonal, which is
and the figure must as:
I try it but the output of figure not as the Required drawing ,
if any prof. can help me thanks alot,
thank you

채택된 답변

DGM
DGM 2021년 8월 30일
편집: DGM 2021년 8월 31일
There are some problems here. Let's start with the solution. I adjusted the parameters so it looks more like the picture, but your parameters should work too.
a1 = 1;
a2 = 2;
a3 = 4;
a4 = 5;
a5 = 6;
s = 0.75;
r = 0.5;
t = 1;
x = 0:0.1:10;
part0 = 0 .* ( x < a1 | x > a5 );
part1 = r*(x-a1)/(a2-a1) .* (x >= a1 & x < a2);
part2 = (r + (t-r)*(x-a2)/(a3-a2)) .* (x >= a2 & x < a3);
%part3 = t .* x==a3; % this point is redundant
part4 = (s + (t-s)*(a4-x)/(a4-a3)) .* (x >= a3 & x < a4);
part5 = s*(a5-x)/(a5-a4) .* (x >= a4 & x <= a5);
G = part0 + part1 + part2 + part4 + part5;
plot(x, G, 'r')
set(gca,'xtick',[0 a1 a2 a3 a4 a5 max(x)], ...
'xticklabel',{'0','a1','a2','a3','a4','a5',num2str(max(x))})
set(gca,'ytick',[0 r s t],'yticklabel',{'0','r','s','t'})
grid on
If you choose to compose the function by masking and adding, then you need to be sure that your masks don't overlap at the endpoints. This includes the single point called part3. I omitted that, since it's redundant. If you choose to add it back in, you'll have to exclude that point from the mask used by part4.
Second, and probably most importantly, the given piecewise function described by the text doesn't match the image. Don't let yourself get run in circles thinking the textbook is always right.
Third, since you smartly chose to assign the peak value to an extra parameter t, you needed to use t in the expressions instead of 1.
  댓글 수: 3
DGM
DGM 2021년 8월 31일
Updated
hasan s
hasan s 2021년 8월 31일
Thank you very very much prof. DGM

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 8월 30일
piecewise() if you have the Symbolic Toolbox.
If you do not have the Symbolic Toolbox, then use logical indexing, such as
mask = a4 <= x & x <= a5
y(mask) = s * (a5 - x(mask) )./(a5 - a4);
Caution: all of your middle endpoints occur multiple times. The value for x == a3 is defined three different ways. The first and second way are not compatible. for x == a3, then (x - a2)/(a3-a2) would be (a3-a2)/(a3-a2) = 1, and that would be multiplied by (1-r) would would give (1-r) . Then 1 minus that is 1 - (1-r) which would be r, which is not the same as the third case where the output has to be 1 when x == a3.
Look as well at the second case when x == a2 . (x-a2)/(a3-a2) when x == a2 would be (a2-a2)/(a3-a2) which would be 0. That would be multiplied by (1-r) giving 0. That would be subtracted from 1, giving 1. That is incompatible with the first expression and incompatible with the diagram, both of which require that the output be r at x == a2.
  댓글 수: 2
hasan s
hasan s 2021년 8월 30일
편집: hasan s 2021년 8월 30일
Thanks alot prof. Walter for your reply
I need it as your great programming in matlab , since I will use it with rand(1,m); for each a1,a2,a3,a4,a5
I thought my mistake was in the equal case only
Your analysis of the function is really great..
ok if I delete the equal from some of them , can I get the function?... Is there a solution to all these problems? to get the Required drawing
hasan s
hasan s 2021년 8월 30일
pardon prof. what you mean "Symbolic Toolbox "

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by