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

3 件のコメント

Daniel Pollard
Daniel Pollard 2021 年 4 月 15 日
Can you give more detail, such us -
  1. Where do these numbers come from? How were they found in the first place?
  2. What have you tried so far?
  3. What sort of calculation do you expect to be inside the for loop?
Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
sorry i forgot to entionb the eqation that we should use for d
  1. d=((34.63/x)-5.162)/2.54
  1. d = [];
  2. for x=[0.1000,0.1500,0.2000]
  3. d=[d ((34.63/x)-5.126)/2.54];
  4. disp ("ANSWER");
  5. end
  6. x=[0.1000 0.1500 0.2000];
  7. fprintf("\t%4g\t\t%4g\n",[x;d])
but when i put this cod into matlab it display like this
for x it shoulkd be 4 decimals .
DGM
DGM 2021 年 4 月 15 日
編集済み: DGM 2021 年 4 月 15 日
Try
fprintf("\t%8.4f\t%8.4f\n",[x;d])
using %g strips insignificant trailing zeros

サインインしてコメントする。

 採用された回答

Daniel Pollard
Daniel Pollard 2021 年 4 月 15 日
編集済み: Daniel Pollard 2021 年 4 月 15 日

0 投票

Your code is
d = [];
for x=[0.1000,0.1500,0.2000]
d=[d ((34.63/x)-5.126)/2.54];
disp ("ANSWER");
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];
disp ("ANSWER");
fprintf("\t%5.4f\t\t%.4f\n", [x(xi);d(xi)])
end

7 件のコメント

Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
answer for your first code
>> Untitled6
ANSWER
ANSWER
ANSWER
0.1 134.32
0.15 88.8743
0.2 66.1512
>>
answer for second code you gave :
>> Untitled3
ANSWER
0.1000 134.3205
ANSWER
0.1500 88.8743
ANSWER
0.2000 66.1512
>> this one coorect accept the fact that there are 3 answer words. it must be 1 answer word and 3 rows and 2 coloms with 4 decimals for x' and d'
Daniel Pollard
Daniel Pollard 2021 年 4 月 15 日
My apologies - try
d = [];
x=[0.1000,0.1500,0.2000];
disp ("ANSWER");
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
Hamada Alkhlif 2021 年 4 月 15 日
編集済み: Hamada Alkhlif 2021 年 4 月 15 日
almost the correct answer after i use the code in matlab i got this answer :
ANSWER
0.1000 134.3205
0.1500 88.8743
0.2000 66.1512
which is almost the same as the output asked above but one thing does not seems right here ,the last 3 numbers of (d') are not alighned , in other word (5 ,3 .2 ) should be alighned vertically .
thank you @Daniel Pollard
DGM
DGM 2021 年 4 月 15 日
Use a wider field width as in the example I gave
fprintf("\t%8.4f\t%8.4f\n",[x;d])
It could be tightened up as necessary.
Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
i did and finally i got the wanted answer .
thank you @DGM
Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
thank you @Daniel Pollard
Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
thanks everybody

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2021 年 4 月 15 日
編集済み: Jan 2021 年 4 月 15 日

0 投票

disp ("ANSWER");
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('Answer:\n');
fprintf("%12g%12g\n", [x, d].')

1 件のコメント

Hamada Alkhlif
Hamada Alkhlif 2021 年 4 月 15 日
1st code gives :
>> Untitled3
ANSWER
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
Answer:
0.1 0.15
0.2 134.32
88.8743 66.1512
>>

サインインしてコメントする。

カテゴリ

製品

リリース

R2021a

タグ

質問済み:

2021 年 4 月 15 日

編集済み:

Jan
2021 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by