Prev: Read data file with comma (instead of decimal point)
Next: Simulink:BusElements with parametric size
From: Joni on 10 May 2010 11:54 Hello. Could someone help me to show how draw sum with matlab. for example: (1/n)+1/(3*n)-1/(5*n)+1/(7*n)-1/(9*n)+... and n=1:100. I can draw a function with only "n+2n+3n+...N*n" but i dont know how to make "n+2n-3n+4n-5n+...N*n" (the + and - change places). thx.
From: Jan Simon on 10 May 2010 18:20 Dear Joni! > Could someone help me to show how draw sum with matlab. > for example: (1/n)+1/(3*n)-1/(5*n)+1/(7*n)-1/(9*n)+... > and n=1:100. > > I can draw a function with only "n+2n+3n+...N*n" but i dont know how to make "n+2n-3n+4n-5n+...N*n" > (the + and - change places). Perhaps this helps: for k = 0:10 x = -1^k end In your examples, the 1st, 2nd, 4th, ... elements are positive. Usually you find the same sign for 1st, 3rd, 5th, ... Jan
From: ImageAnalyst on 10 May 2010 18:40
Joni: For the second question, how about this: % Plot n+2n-3n+4n-5n+...N*n % Generate 50 n's for the x (horizontal) axis. n = linspace(-10,10,50) % Plot between -10 and +10 N = 25; % 25 terms for example. coeffs = 1:N; % 1 2 3 4 ... 25 coeffs(3:2:N) = -coeffs(3:2:N) % Every other one starting with 3 is negative. % factor out n to create y = n * (1+2-3+4-5 ... -25) for k = 1:length(n) % for all 50 n's, get the sum of the 25 terms. y(k) = n(k) * sum(coeffs); end % Plot the 50 n,y pairs. plot(n, y); Note how I used coeffs(3:2:N) = -coeffs(3:2:N) to negate every other one (starting with term #3 rather than term #1 for some reason). Now, try to tackle your first question on your own, now that you know how to use that trick (if you can call it that). |