Display maximal value in a range?

1 Ansicht (letzte 30 Tage)
Desiree
Desiree am 10 Aug. 2019
Kommentiert: Star Strider am 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!

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 10 Aug. 2019
Bearbeitet: KALYAN ACHARJYA am 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 Kommentar
Desiree
Desiree am 10 Aug. 2019

Thanks for your help too! This also was helpful

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by