FOR LOOP MATLAB
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 4 Aug. 2011
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.
Weitere Antworten (1)
Walter Roberson
am 4 Aug. 2011
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 Kommentare
Walter Roberson
am 4 Aug. 2011
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.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown 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!