how to get data from an interval

Hi, I need to use the y values between the x=50 and x=180 values to do another plot. How can I get these vaules from the current plot? I've readed some answers using the 'findobj' function but I don't know if it's the correct function for my task and I don't understand how to use it in my specific situation.
Could someone help me please?
here's my code:
clc;
clear;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P=0.5*800000;%kN -carico agente
R=20;%mm -raggio medio
L=3*75;%mm -lunghezza tubo
s=1;%mm -spessore di parete
ni=0.3;% -coeff poisson
E=210000;%MPa -modulo young
w=2;%MPa -pressione int/ext
K=0.5;% -incasto incastro
%equazioni derivanti dall'equilibrio radiale:
E1=E/(1-(ni^2));% -modulo young dilataz laterale impedita
I=(2*pi*R*(s^3))/12;% -momento di inerzia cilindro
k=2*pi*((E*s)/R);% -rigidezza molla fondazione
Pcr=sqrt(4*k*E1*I)% -carico critico
Pcre=((pi)^2*E*I)/(K*((L/1000)^2))% -carico critico secondo Eulero
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = @(z,u) [u(2); u(3); u(4); (-1/(E1*I))*((P*u(3))+(k*u(1)))]; %equazione differenziale w=0
bc = @(ua, ub) [ua(1); ua(2)-0.00000001; ub(1); ub(2)+0.00000001];
zmesh = linspace(0, L, 100000);%crea vettore equispaziato da 0->lunghezza cilindro
iguess= @(z) [0; 0; 0; 0];%initial guess @soluzione dalla quale inizia l'iterazione della function bvp4c
solinit = bvpinit(zmesh, iguess); %reference @doc bvpinit (form initial guess value)
opts = bvpset('RelTol',0.1,'AbsTol',0.1,'Stats','on'); %setting per ottenere le statistiche della function bvp4c
sol= bvp4c(f, bc, solinit,opts)% sol in x, y values
figure
hold on
grid on
plot(sol.x(1,:),sol.y(1,:),'.') %I need the y values from this plot for x from 50 to 180
legend('w=0');
xlabel('z');
ylabel('solution u');

 Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Sep 2020
Edited: Ameer Hamza on 26 Sep 2020
You can use logical indexing. Add these lines at the end of your code
x = sol.x;
y = sol.y;
idx = (x > 50) & (x < 180);
figure
hold on
grid on
plot(x(idx), y(1, idx))

6 Comments

Is it also possibile to get the max of the y values from this new range?
Yes,
x = sol.x;
y = sol.y;
idx = (x > 50) & (x < 180);
x_new = x(idx);
y_new = y(1, idx);
figure
hold on
grid on
plot(x_new, y_new)
y_new_max = max(y_new);
I'm sorry for bothering you but I have another problem.
I need to run the code written in the first question for the variable "P" running from 400000 to 900000, then consider only the max( sol.y) value considered in the sol.x>50 and sol.x<180 range for every "P(i)" solution.
i've tried to fit the code in a for loop i=1:length(n) allocating a P(i) value to the P variable where n is a "
linspace" vector but I don't know if it is correct.
Cuold you help me please?
this is the for loop that should solve the differential equation f for every P value:
n=linspace(400000,900000);
for i=1:length(n)
f = @(z,u) [u(2); u(3); u(4); (-1/(E1*I))*((P(i)*u(3))+(k*u(1)))];
bc = @(ua, ub) [ua(1); ua(2)-0.00000001; ub(1); ub(2)+0.00000001];
zmesh = linspace(0, L, 100000);
iguess= @(z) [0; 0; 0; 0];
solinit = bvpinit(zmesh, iguess);
Sol= bvp4c(f, bc, solinit);
end
Instead of n, assign the linspace values to P.
P = linspace(400000,900000);
y_max_values = zeros(size(P));
for i=1:length(P)
f = @(z,u) [u(2); u(3); u(4); (-1/(E1*I))*((P(i)*u(3))+(k*u(1)))];
bc = @(ua, ub) [ua(1); ua(2)-0.00000001; ub(1); ub(2)+0.00000001];
zmesh = linspace(0, L, 100000);
iguess= @(z) [0; 0; 0; 0];
solinit = bvpinit(zmesh, iguess);
Sol= bvp4c(f, bc, solinit);
x = Sol.x;
y = Sol.y;
idx = (x > 50) & (x < 180);
x_new = x(idx);
y_new = y(1, idx);
y_max_values(i) = max(y_new);
end
running this I get this error :
Unable to resolve the name sol.x.
Error in Untitled (line 36)
% x = sol.x;
Please check the code in the updated comment. You were using Sol as a variable name. I mistakenly typed sol.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!