# FOR LOOP , beginner question.

1 view (last 30 days)
Hamada Alkhlif on 15 Apr 2021
Edited: Jan on 15 Apr 2021
i want to Write a code or script including a FOR LOOP in order to computing the value of d for the following values of x and returning an output variable named ANSWER just as shown : x = 0.10, x = 0.15, and x = 0.20
DGM on 15 Apr 2021
Try
fprintf("\t%8.4f\t%8.4f\n",[x;d])
using %g strips insignificant trailing zeros

Daniel Pollard on 15 Apr 2021
Edited: Daniel Pollard on 15 Apr 2021
d = [];
for x=[0.1000,0.1500,0.2000]
d=[d ((34.63/x)-5.126)/2.54];
end
x=[0.1000 0.1500 0.2000];
fprintf("\t%4g\t\t%4g\n",[x;d])
If I understand right, you want
d = [];
x=[0.1000,0.1500,0.2000];
for xi = 1:numel(x)
d=[d ((34.63/x(xi))-5.126)/2.54];
fprintf("\t%5.4f\t\t%.4f\n", [x(xi);d(xi)])
end
Hamada Alkhlif on 15 Apr 2021
thanks everybody

### More Answers (1)

Jan on 15 Apr 2021
Edited: Jan on 15 Apr 2021
for x = [0.10, 0.15, 0.20]
d = ((34.63 / x) - 5.126) / 2.54;
fprintf("%12g%12g\n", x, d)
end
Or:
x = [0.10, 0.15, 0.20]
d = ((34.63 ./ x) - 5.126) / 2.54; % .7 for elementwise division
fprintf("%12g%12g\n", [x, d].')
##### 1 CommentShowHide None
Hamada Alkhlif on 15 Apr 2021
1st code gives :
>> Untitled3
0.1 134.32
0.15 88.8743
0.2 66.1512
>> the answer here does not have 4 decimals for x' and not alighed vertically , same for d'
2nd code gives :
>> Untitled3
x =
0.1000 0.1500 0.2000
0.1 0.15
0.2 134.32
88.8743 66.1512
>>

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by