For Loop answer into a vector

4 vues (au cours des 30 derniers jours)
Micheá McDonagh
Micheá McDonagh le 29 Sep 2021
Réponse apportée : Dave B le 29 Sep 2021
See my code below. I want to run a for loop that will loop through the eqn y 20 times with the value of x that is gotten from the linspace.
Then i want to save each value of y from the loop, into a vector. Can someone help me please. Hope that makes sense.
clc, clear all, close all
b = 10;
d = 10;
E = 207E9;
p = -8000;
I = (b*d^3)/12;
l = 227;
x = linspace(1,226,20);
for i = 1:length(x)
y = (p*x^2*(3*l-x)/(6*E*I))
end
  1 commentaire
David Hill
David Hill le 29 Sep 2021
b = 10;
d = 10;
E = 207E9;
p = -8000;
I = (b*d^3)/12;
l = 227;
x = linspace(1,226,20);
y = (p.*(x.^2).*(3*l-x)./(6*E*I));

Connectez-vous pour commenter.

Réponse acceptée

Dave B
Dave B le 29 Sep 2021
b = 10;
d = 10;
E = 207E9;
p = -8000;
I = (b*d^3)/12;
l = 227;
x = linspace(1,226,20);
% not necessary, but good:
y = nan(size(x));
for i = 1:length(x)
y(i) = (p*x(i)^2*(3*l-x(i))/(6*E*I));
end
disp(y)
1.0e-03 * -0.0000 -0.0009 -0.0031 -0.0066 -0.0114 -0.0174 -0.0244 -0.0325 -0.0415 -0.0513 -0.0619 -0.0732 -0.0851 -0.0976 -0.1106 -0.1239 -0.1376 -0.1514 -0.1655 -0.1796
% But you didn't need the for loop, you should just do it the MATLAB way!
y = (p.*x.^2.*(3*l-x)/(6*E*I));
disp(y)
1.0e-03 * -0.0000 -0.0009 -0.0031 -0.0066 -0.0114 -0.0174 -0.0244 -0.0325 -0.0415 -0.0513 -0.0619 -0.0732 -0.0851 -0.0976 -0.1106 -0.1239 -0.1376 -0.1514 -0.1655 -0.1796

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by