Display maximal value in a range?

1 view (last 30 days)
Desiree
Desiree on 10 Aug 2019
Commented: Star Strider on 10 Aug 2019
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!

Accepted Answer

Star Strider
Star Strider on 10 Aug 2019
Edited: Star Strider on 10 Aug 2019
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 Comments
Desiree
Desiree on 10 Aug 2019
Once again thanks a lot for the help!
Star Strider
Star Strider on 10 Aug 2019
As always, my pleasure!

Sign in to comment.

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Aug 2019
Edited: KALYAN ACHARJYA on 10 Aug 2019
"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 Comment
Desiree
Desiree on 10 Aug 2019

Thanks for your help too! This also was helpful

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!