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.
|
|
0
|
|
|
|
Reply
|
Kevin
|
3/23/2010 1:48:19 PM |
|
"Kevin " <kevin_stanza@yahoo.co.uk> wrote in message
news:hoagr3$l5h$1@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@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|
|
0
|
|
|
|
Reply
|
Steven
|
3/23/2010 2:43:59 PM
|
|