How can I plot a conditional function?

조회 수: 6 (최근 30일)
Duncan Cameron
Duncan Cameron 2015년 3월 2일
편집: Andrei Bobrov 2015년 3월 3일
I am trying to combine logarithmic (BH2) and polynomial (BH1) trendlines into a blended trendline (BH3) that passes through the origin. My current code plots BH3 for the final condition for all t. Please could someone offer corrected code? Here are functions and problematic code is below. Many thanks in advance.
  • BH1: y = 7.1851*ln(t)-2.62
  • BH2: y = -0.06*t^2+2.185*t
  • BH3: 0<t<=3.07 y= BH2 3.07<t<=7.92 y= 0.67*BH2+0.33*BH1 t>7.92 y= 0.5*BH1+0.5*BH2
t = 0:0.5:30; %define time vector
BH1 = 7.81*log(t)-2.625; %define logarithmic trend
BH2 = -0.06*t.^2+2.19*t; %define polynomial trend
%now define combination of trends
if t <= 3.07 %force through origin
BH3 = BH2;
elseif 3.07 < t <= 7.92; %intial blend
BH3 = 0.67*BH2 + 0.33*BH1;
else %equal blend
BH3 = 0.5*BH2 + 0.5*BH1;
end
plot(t,BH1,'r',t,BH2,'r',t,BH3,'b');

채택된 답변

Andrei Bobrov
Andrei Bobrov 2015년 3월 2일
편집: Andrei Bobrov 2015년 3월 3일
t2 = 3.07 < t & t <= 7.92; % EDIT
t3 = t > 7.92;
BH3 = BH2;
BH3(t2) = BH3(t2)*.67 + BH1(t2)*.33;
BH3(t3) = BH3(t3)*.5 + BH1(t3)*.5;
  댓글 수: 2
Duncan Cameron
Duncan Cameron 2015년 3월 2일
Thank you for your quick response. After making some changes (BH3 should be BH2 lines 4&5) to the code (see below) I am getting the following plot. There is clearly something wrong as the red line should be identical to the green line for 0 < t <= 3.07.
Can you identify the problem? Code:
t1 = t < 3.07;
t2 = 3.07 < t <= 7.92;
t3 = t > 7.92;
BH3(t1) = BH2(t1);
BH3(t2) = BH2(t2)*.67 + BH1(t2)*.33;
BH3(t3) = BH2(t3)*.5 + BH1(t3)*.5;
plot(t,BH1,'b',t,BH2,'g',t,BH3,'r');
Andrei Bobrov
Andrei Bobrov 2015년 3월 3일
Sorry, my mistake, fixed

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

추가 답변 (1개)

Adam
Adam 2015년 3월 2일
BH3( t <= 3.07 ) = BH2;
blendIndices = 3.07 < t && t <= 7.92;
BH3( blendIndices ) = 0.67 * BH2( blendIndices ) + 0.33 * BH1( blendIndices );
BH3( t > 7.92 ) = 0.5 * BH2( t > 7.92 ) + 0.5 * BH1( t > 7.92 );
should work I think though I wrote that off my head without testing it on command line. Hopefully you get the idea though.
t is a vector of values so you can't test it in an if statement in the way you are because the statement
t <= 3.07
is actually a logical vector of the same length as t, not a single value that can be tested for an if statement. The above solution uses logical indexing to achieve what I think you want to achieve.
  댓글 수: 2
Duncan Cameron
Duncan Cameron 2015년 3월 2일
Thanks for your quick response. Unfortunately Matlab has returned an error, following correction of one error I am not sure how to proceed with this one below.
BH3( t <= 3.07 ) = BH2( t <= 3.07 );
blendIndices = 3.07 < t && t <= 7.92;
BH3( blendIndices ) = 0.67 * BH2( blendIndices ) + 0.33 * BH1( blendIndices );
BH3( t > 7.92 ) = 0.5 * BH2( t > 7.92 ) + 0.5 * BH1( t > 7.92 );
Operands to the and && operators must be convertible to logical scalar values.
Error in Trends (line 8) blendIndices = 3.07 < t && t <= 7.92;
Adam
Adam 2015년 3월 3일
Use & instead of &&

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by