From: blup_00 on
Hello, Im new at matlab and I have problem with for function

z=input('z=');
n=input('n=');
y=input('y=');
cs = 1;
cz =1;
for j=1:z
x=input('x=');
for i=2:2:n
cn = cs * -(x^2)/((i-1)*i);
cz = cz + cn;
cs = cz;
if cz>y
break
end
disp(x); disp(cz);
end
end

In this program I need that first 'for' starts, inputs x and that second 'for' runs fully (n times) for x variable, displays cz and then all over again z times.

But when I run it, first for starts, second for starts and dont run fully (only runs for i=2 and I need to run comletely).
What should I change?
Thanks in advance
From: Bruno Luong on
blup_00 <borisziv(a)gmail.com> wrote in message <315334386.59151.1272820697760.JavaMail.root(a)gallium.mathforum.org>...
>
> In this program I need that first 'for' starts, inputs x and that second 'for' runs fully (n times)

isn't rather floor(n/2) times?

Because you don't provide user inputs, who could tell how your program should behaves?

The better even is not asking question, but to teach yourself how to use Matlab Debugger capability, step through the program, and check whereas the variable taking the values you expected.

Bruno
From: ImageAnalyst on
The second for won't run n times because the ending value is n and
your increment is 2. You should change it to
for i=1:n
You need to pull out disp(cz); out of the loop if you only want it to
print cz after the second loop has finished. Put it after the inner
for loop's end and before the outer for loop's end statement.

Then, the second for loop runs only once because cz > y and it hits
the break and bails out of that loop. I don't know why - it depends
on your values. Set a breakpoint there and find out why.

Finally, change your inner loop iteration index from i to k or some
other letter than i and j, which are imaginary numbers (it's bad
practice to use them as loop iterators).