Guys I am so sorry i didn't the complete the question before, i was thinking if i can just get started i will be able to continue but i am more confused now...
The full question is: Write 4 lines of code to use a forloop in the variable i to calculate and store the values of t and h=(3+t)t. Continuing with the above and assuming the out variable is h, allocate memory with NaN command for t and h. b. Next we want to change the problem to calculate 3 equations in the same for loop above. The input will still be t but now the 3 output equations are: h1=10cos(t) h2=t+2 h3=sin(t) Assuming we have allocated memory for h1,h2 and h3, what would the new code for the body of the loop be?

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 4 日

0 投票

Open MATLAB and type "help for" in the Command Window, read the text and there is a good example to show you how to do it.
My guess is that your professor wants you to learn the basics of MATLAB programming.
First:
for i=1:1000
t(i)=i*0.1; % t probably is the time, increased by 0.1 seconds
h(i)=(3+t(i))*t(i);
end
This approach will be slow when the iteration increases because t and h are not pre-allocated.
Second:
t=nan(1000,1);
h=nan(1000,1);
for i=1:1000
t(i)=i*0.1; % t probably is the time, increased by 0.1 seconds
h(i)=(3+t(i))*t(i);
end
Third
I hope you can do it yourself. I think I've shown you the door.

2 件のコメント

Oluakin
Oluakin 2011 年 8 月 4 日
Thanks
Oluakin
Oluakin 2011 年 8 月 4 日
Guys I am so sorry i didn't complete the question before, i was thinking if i can just get started i will be able to continue but i am more confused now...
The full question is: Write 4 lines of code to use a forloop in the variable i to calculate and store the values of t and h=(3+t)t. Continuing with the above and assuming the out variable is h, allocate memory with NaN command for t and h. b. Next we want to change the problem to calculate 3 equations in the same for loop above. The input will still be t but now the 3 output equations are: h1=10cos(t) h2=t+2 h3=sin(t) Assuming we have allocated memory for h1,h2 and h3, what would the new code for the body of the loop be?

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 8 月 4 日

2 投票

For the first part:
for i = 1
t = t;
h = (3+t)*t;
end
Perhaps you omitted key parts of the question that described what t is, or how far many iterations the loop was supposed to go?

3 件のコメント

Oluakin
Oluakin 2011 年 8 月 4 日
Thanks Walter Roberson: This helped to get start
Yes I did omitted part of the question the full question was
Question: Write 4 lines of code to use a forloop in the variable i to calculate and store the values of t and h=(3+t)t. Continuing with the above and assuming the out variable is h, allocate memory with NaN command for t and h.
I was thinking if i can get started i can finish it but please take a look at it if there is other help you can offer on the rest of the question
Walter Roberson
Walter Roberson 2011 年 8 月 4 日
That is the same question as you originally posted. It asks you to calculate t, but does not give any information about how t is to be calculated.
To finish the question *in the form posted*, after the "for" loop, add the two statements,
t = nan;
h = nan;
I do not understand the point of allocating memory for h *after* the loop instead of *before* the loop, but the allocating the memory _before_ the loop would not be "continuing on" as the question requires.
Oluakin
Oluakin 2011 年 8 月 4 日
The full question is:
Write 4 lines of code to use a forloop in the variable i to calculate and store the values of t and h=(3+t)t. Continuing with the above and assuming the out variable is h, allocate memory with NaN command for t and h.
b. Next we want to change the problem to calculate 3 equations in the same for loop above. The input will still be t but now the 3 output equations are:
h1=10cos(t)
h2=t+2
h3=sin(t)
Assuming we have allocated memory for h1,h2 and h3, what would the new code for the body of the loop be?

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

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by