From: Kevin on
im calculating a geometric progression and need to know how to display is it ar0+ar1+ar2+...+arn
this is the code i have:

x=5;
syms x
for i=1:19
x=x+5*(1/2)^i
end

but i want the display to show 5+5/2+15/4 and so on, i just need to know how to display it in this kind of format.
From: Steven Lord on

"Kevin " <kevin_stanza(a)yahoo.co.uk> wrote in message
news:hoagr3$l5h$1(a)fred.mathworks.com...
> im calculating a geometric progression and need to know how to display is
> it ar0+ar1+ar2+...+arn
> this is the code i have:
>
> x=5;
> syms x

This just overwrote the value of x that you assigned previously. If you
really want x to be the symbolic value 5:

x = sym(5);

> for i=1:19
> x=x+5*(1/2)^i
> end
>
> but i want the display to show 5+5/2+15/4 and so on, i just need to know
> how to display it in this kind of format.

FPRINTF.

x = sym(5);
two = sym(2);
fprintf('5');
for k = 1:19
added = two^k;
x = x+5/added;
fprintf('+5/%s', char(added));
end
fprintf('\n');

Note that x will not contain the individual terms; it will instead contain
the sum of the terms. If you want to store the individual terms, create a
sym array and store each term in an element, then sum them up after the loop
is finished.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ