Filter löschen
Filter löschen

I need to know how to use the for command to calculate y=2/x

3 Ansichten (letzte 30 Tage)
Saif
Saif am 1 Dez. 2022
Kommentiert: Walter Roberson am 4 Dez. 2022
I am just now learning how to use for loops but I am unsure how to use it to solve an equation with two unknowns. I need to know how to use the for command to calculate y/2/x.

Antworten (2)

Benjamin Thompson
Benjamin Thompson am 1 Dez. 2022
x = 1:10;
y = 2 ./ x;

Walter Roberson
Walter Roberson am 4 Dez. 2022
You should learn this pattern:
xvals = 1:0.5:10;
num_x = length(xvals);
y = zeros(num_x, 1);
for xindex = 1 : num_x
x = xvals(xindex);
y(xindex) = 2./x;
end
plot(xvals, y)
  1 Kommentar
Walter Roberson
Walter Roberson am 4 Dez. 2022
If you use this pattern where you loop over indices rather than over content then the content does not need to be in any particular order, and can contain duplicates. For example,
xvals = rand(1,20) * 2 - 1;
num_x = length(xvals);
y = zeros(num_x, 1);
for xindex = 1 : num_x
x = xvals(xindex);
y(xindex) = 2./x;
end
plot(xvals, y, '*')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by