Display maximal value in a range?

조회 수: 4 (최근 30일)
Desiree
Desiree 2019년 8월 10일
댓글: Star Strider 2019년 8월 10일
Hello. I would like to display the max(abs(sigma)) from around t=7 to t=10 from this code. The plot is from 0 to 10 and I tried to display that max value but it shows me the maximum value from the whole range (from 0 to 10) instead from 7 to 10. How can I do it? Here’s my code:
clear all
t=0;% initial time
x=[1,1,1];% initial condition for t=0
alpha=30;
tau=0.0001;
k=1; % counter
while t<=10
f=-sin(t+5)-0.5*cos(t)-x(2);
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
vcc=cos(t+1)+0.5*sin(0.5*t-2);
sigma=v-vc;
sigmadot=f-vcc;
u=alpha*((abs(sigmadot)).^2*sign(sigmadot)+sigma)/((sigmadot).^2+abs(sigma));
dx=[f,cos(1+x(1)+x(3))+(2-cos(x(1)+x(3)+1))*u,cos(x(1)+0.5*x(3)-t)-x(3)+u];
x=x+dx*tau;
t=t+tau;
sigmav(k)=sigma;
sigmadotv(k)=sigmadot;
tv(k)=t;
k=k+1;
end
figure
plot(tv,sigmav,'b',tv,sigmadotv,'r')
Help is appreciated!

채택된 답변

Star Strider
Star Strider 2019년 8월 10일
편집: Star Strider 2019년 8월 10일
I do not see any max function calls, so I am not certain what result you want.
Displying only the values for ‘tv’ between 7 and 10 is straightforward:
Lidx = (tv>=7) & (tv<=10);
figure
plot(tv(Lidx),sigmav(Lidx),'b',tv(Lidx),sigmadotv(Lidx),'r')
with ‘Lidx’ being the logical index vector. This will select the values of the vectors you want.
To display ‘max(abs(sigma))’, assuming you intend ‘sigmav’:
maxSigma = max(abs(sigmav(Lidx)))
maxSigma =
4.451070235722554e-07
If you simply want to restrict the plot, you can also do that with the xlim function.
If you want to display it on the plot, use the text function.
  댓글 수: 3
Desiree
Desiree 2019년 8월 10일
Once again thanks a lot for the help!
Star Strider
Star Strider 2019년 8월 10일
As always, my pleasure!

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

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 8월 10일
편집: KALYAN ACHARJYA 2019년 8월 10일
"I would like to display the max(abs(sigma)) from around t=7 to t=10 from this code"
Is this one:
t=0;% initial time
x=[1,1,1];% initial condition for t=0
alpha=30;
tau=0.0001;
k=1; % counter
l=1;
while t<=10
f=-sin(t+5)-0.5*cos(t)-x(2);
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
vcc=cos(t+1)+0.5*sin(0.5*t-2);
sigma=v-vc;
%% I haved changed Here
if t>=7 & t<=10
sigma_update(l)=sigma;
l=l+1;
end
%%
sigmadot=f-vcc;
u=alpha*((abs(sigmadot)).^2*sign(sigmadot)+sigma)/((sigmadot).^2+abs(sigma));
dx=[f,cos(1+x(1)+x(3))+(2-cos(x(1)+x(3)+1))*u,cos(x(1)+0.5*x(3)-t)-x(3)+u];
x=x+dx*tau;
t=t+tau;
sigmav(k)=sigma;
sigmadotv(k)=sigmadot;
tv(k)=t;
k=k+1;
end
figure
plot(tv,sigmav,'b',tv,sigmadotv,'r');
disp(max(abs(sigma_update)));
Result:
4.451070235722554e-07
  댓글 수: 1
Desiree
Desiree 2019년 8월 10일

Thanks for your help too! This also was helpful

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

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by