I am trying to find the roots of a function, with a parameter that changes. The function is that of a parabola
V=rand(10,1)
g=9.81
z0=500
I should use the following expression in for loop right?
t_roots=roots([-.5*g,V,z0])

Réponses (1)

Askic V
Askic V le 6 Fév 2023

0 votes

Yes, if V contains paramters, then the could would look like this:
V = rand(10,1);
g = 9.81;
z0 = 500;
for vi = 1: numel(V)
t_roots = roots([-0.5*g,vi,z0])
end
t_roots = 2×1
10.1988 -9.9950
t_roots = 2×1
10.3023 -9.8946
t_roots = 2×1
10.4068 -9.7952
t_roots = 2×1
10.5124 -9.6969
t_roots = 2×1
10.6189 -9.5995
t_roots = 2×1
10.7265 -9.5033
t_roots = 2×1
10.8351 -9.4080
t_roots = 2×1
10.9448 -9.3138
t_roots = 2×1
11.0554 -9.2205
t_roots = 2×1
11.1671 -9.1283

5 commentaires

Giovanni Curiazio
Giovanni Curiazio le 6 Fév 2023
Modifié(e) : Giovanni Curiazio le 6 Fév 2023
Thank a lot. Now if i want to plot the different parabolas , how i do that? I should put the x,y,x distances also in a loop?
This is just a small example, but it should be sufficient to show you:
V = rand(3,1);
g = 9.81;
z0 = 500;
x = -12:0.1:12;
for vi = 1: numel(V)
t_roots = roots([-0.5*g,vi,z0]);
y = -0.5*g*x.^2+vi*x+z0;
plot(x, y);
hold on;
end
OK, I should have explained myself better. Now I will put what I got , and I will try to explain better what I want to get
%% Parabola for 1 piece
g=9.80665; %agravity
z0= 14325; %starting height
x0=0;
y0=0;
V_0=200*.3048; %starting velocity
gamma=0.78;
DeltaV_c=randn(10,3); %Speed increments in the three directions x, y, z
% Debris velocity components after the explosion
V_0x=V_0*cos(gamma)+DeltaV_c(1,1);
V_0y=0+DeltaV_c(1,2);
V_0z=V_0*sin(gamma)+DeltaV_c(1,3);
%debris flight time
t_radici=roots([-.5*g,V_0z,z0])
t_radici = 2×1
58.5698 -49.8805
t = linspace(0,t_radici(t_radici>0),1000);
%distanza percorsa nelle tre direzioni
x=x0+V_0x*t;
y=y0+V_0y*t;
z=z0+V_0z*t-.5*g*t.^2;
figure(3);
plot3(x,y,z)
hold on
plot3(x0,y0,z0,'ro','MarkerSize',5)
plot3(x(end),y(end),z(end),'ro','MarkerSize',5)
xlabel('Posizione x (m)')
ylabel('Posizione y (m)')
zlabel('Posizione z (m)')
grid on
%ground impact point
x_f=x(end)
x_f = 2.6214e+03
y_f=y(end)
y_f = 17.2835
z_f=z(end)
z_f = 0
Giovanni Curiazio
Giovanni Curiazio le 6 Fév 2023
Modifié(e) : Giovanni Curiazio le 6 Fév 2023
Now I would like to achieve the same thing but considering more debris: so the starting height remains the same but at the initial velocity I would like to add up all the elements of DeltaV_c (for each direction). And finally plot the different parabolas. In fact as you can see, in the code I used only the first line of DeltaV_c, now I would like to use all the lines so that I have 10 different speeds, so 10 parabolas
I guess you are trying to achive something like this?
g=9.80665; %agravity
z0= 14325; %starting height
x0=0;
y0=0;
V_0=200*.3048; %starting velocity
gamma=0.78;
DeltaV_c = randn(10,3); %Speed increments in the three directions x, y, z
% Debris velocity components after the explosion
[r,c] = size(DeltaV_c);
for i = 1:r % each row contains increments in directions
V_0x=V_0*cos(gamma)+DeltaV_c(i,1);
V_0y=0+DeltaV_c(i,2);
V_0z=V_0*sin(gamma)+DeltaV_c(i,3);
%debris flight time
t_radici = roots([-.5*g,V_0z,z0]);
t = linspace(0,t_radici(t_radici>0),1000);
%distanza percorsa nelle tre direzioni
x=x0+V_0x*t;
y=y0+V_0y*t;
z=z0+V_0z*t-.5*g*t.^2;
plot3(x,y,z)
hold on
plot3(x0,y0,z0,'ro','MarkerSize',5)
plot3(x(end),y(end),z(end),'ro','MarkerSize',5)
xlabel('Posizione x (m)')
ylabel('Posizione y (m)')
zlabel('Posizione z (m)')
grid on
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by