For Loops and Plotting

The following is a for loop. Just checking to make sure it is set up right. Also, when I go to plot this array and an array from 0 to 250 by 0.5, it doesn't work. In my code, it is "plot(timearray,resultarray)" and doesn't work. Any suggestions?
Thanks
for i = (0:.5:250)
resultarray = (((array(6,:) * array(2,:)) / (constant - array(6,:))) * (exp(-array(6,:) * i * 3600) - exp(-constant * i * 3600))) + (array(1,:) * exp(-constant * i * 3600));
end

 Accepted Answer

You need to subscript ‘resultarray’, and also ‘timearray’, to create a vector out of each one:
for k1 = (0:.5:250)
resultarray(k1) = (((array(6,:) * array(2,:)) / (constant - array(6,:))) * (exp(-array(6,:) * i * 3600) - exp(-constant * i * 3600))) + (array(1,:) * exp(-constant * i * 3600));
end
I changed your loop index counter to ‘k1’ because MATLAB uses i and j as its imaginary operators.
That is particularly significant here, because if ‘i’ is an index will produce the exponent of a real argument, exp(-constant*i*3600), but if it you intend it to be the imaginary operator, will return cos(constant-3600)-i*sin(constant*3600).
I left it as ‘i’ in your calculation for ‘resultarray’ because I have no idea what you intend. Neither does MATLAB.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 7 Dec 2014

Answered:

on 7 Dec 2014

Community Treasure Hunt

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

Start Hunting!