FOR LOOP MATLAB

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?

 采纳的回答

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 个评论

Thanks
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 个)

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 个评论

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
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.
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 中查找有关 Startup and Shutdown 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by