code loop occur errors

m = 0.5;
g = 9.81;
F = m*g;
theta = 0:50
V = sqrt(2*F/m);
vertical = V*sin(theta);
pve = V_hi > 0;
t_pve = theta(pve);
for i = 1:length(V_vi)
for j = 1:length(t_pve)
j = j+1;
while vertical(i) > 4.65
vertical(i) = Vertical_new;
end
end
end
I wanna obtain all the Vertical_new which satisfied the while loop condition, why it won't run? This is the error shown
Unrecognized function or variable 'Vertical_new'

9 Commenti

Torsten
Torsten il 20 Mag 2022
V_hi, V_vi, Vertical_new are not defined anywhere in the above code.
m = 0.5;
g = 9.81;
F = m*g;
theta = 0:50
V = sqrt(2*F/m);
vertical = V*sin(theta);
pve = Vertical > 0;
t_pve = theta(pve);
for i = 1:length(Vertical)
for j = 1:length(t_pve)
j = j+1;
while vertical(i) > 4.65
vertical(i) = Vertical_new;
end
end
end
m = 0.5;
g = 9.81;
F = m*g;
theta = 0:50
V = sqrt(2*F/m);
vertical = V*sin(theta);
pve = vertical > 0;
t_pve = theta(pve);
for i = 1:length(vertical)
for j = 1:length(t_pve)
while vertical(i) > 4.65
vertical(i) = Vertical_new;
end
end
end
You have to define Vertical_new.
John fredson
John fredson il 20 Mag 2022
Can you further explain?
Torsten
Torsten il 20 Mag 2022
First, use "sind" instead of "sin" because your theta values are in degrees, not in radians.
What's your aim ? Do you want to extract those components of the vector "vertical" which are > 4.65 ? Maybe it's easier to look at the problem than to look at the code.
John fredson
John fredson il 20 Mag 2022
Torsten
Torsten il 20 Mag 2022
Modificato: Torsten il 20 Mag 2022
m = 0.5;
g = 9.81;
F = m*g;
theta = 0:50
V = sqrt(2*F/m);
vertical = V*sind(theta);
Vertical_new = vertical(vertical > 4.65)
extracts the elements of the vector "vertical" which are > 4.65. Is it that what you mean ?
John fredson
John fredson il 20 Mag 2022
Modificato: John fredson il 20 Mag 2022
yup, means that vertical > 4.65 will be put into Vertical_new
...
for i = 1:length(V_vi)
for j = 1:length(t_pve)
j = j+1;
while vertical(i) > 4.65
vertical(i) = Vertical_new;
end
end
end
Besides the other comments, there's nothing inside the loop over j that is dependent upon what j is; it simply repeats the same assignment of i element of array length(t_pve) times.
Secondly, you try to change the loop indexing variable j each time inside the loop -- this is not kosher; see the documentation for for that has an explanation/note about it. Even if it were used, the loop construct would reset it to the loop counter value each pass through the loop, anyway...
Thirdly, you referenced a variable V_hi that looks to have been intended to be the height against to test; but as others noted, it's not defined. Assign it the value of the constant and don't bury "magic" numbers of that sort in the code; use variables that can be changed easily/documented.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 20 Mag 2022

Commentato:

dpb
il 20 Mag 2022

Community Treasure Hunt

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

Start Hunting!

Translated by