View Full Version : Matlab Help - Taylor Series
gap9q3
10 Mar 2009, 8:21am
I am studying and I cannot figure out how to do this practice problem... Can ANYONE help me please! Thanks! :D
The function 1/(1-x) can be approximated using a specific type of Taylor Series as follows:
1/(1-x)≈Σ(where n=0, and k=∞)=x^0+x^1+x^2+x^3....
Write a program to calculate the series approximation using a "for" loop running from 1 to k. Also, calculate the correct value. Test the code for k=5 & K=10 with x=0.5.
Thanks a lot, I've been working on this for a while, and don't usually get stuck with programming, I just haven't learned the "for" loop too well yet.
shwaip
10 Mar 2009, 5:25pm
post the code of any attempt you've made, and we'll help you out.
gap9q3
10 Mar 2009, 5:32pm
post the code of any attempt you've made, and we'll help you out.
k=5
x=0.5
k=10
x=0.5
for n = 0:k
x(n) = x(n-1) + x^n
end
shwaip
10 Mar 2009, 5:53pm
close.
matlab uses 1-based indexing, so the first element of the array is x(1).
i'd do something like this:
k=5
x=0.5
est=0;
for n=0:k
est = est + x^n;
end
gap9q3
10 Mar 2009, 5:59pm
close.
matlab uses 1-based indexing, so the first element of the array is x(1).
i'd do something like this:
k=5
x=0.5
est=0;
for n=0:k
est = est + x^n;
end
THANKS!! I think I got it! :D
My friend and I have one more we can't figure out, if you are willing to help. I'll give you the problem, original code template, and what I have so far.
An m-file, for_sines.m, with minimal documentation is in the classwork folder – add code to generate and plot the 3 sine waves from Lab 4 using"for" loops. Recall that we plotted sin(2pft) vs. t for f = 6, 8, and 10 Hz and t running from 0 to 2.5 sec in 0.01 sec increments. Lab 4 Assignment is in Lecture 4 in the "classwork" folder. The t array and sine array are initialized in for_sines.m . Other than this initialization, don't use any vector initialization; that is, generate both the final time and frequency vectors and the 3 sine waves using loops. Generate the 3 subplots using a loop. Your code will yield the same graph as before; however, now include frequency is in the title.
Template:
% for_sines.m
% yourname and lab section
fprintf('\nyourname labsection\n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
% initialize f to its proper values using a "for" loop
;
% initialize t to its proper values using a "for" loop
;
% calculate the sine and plot using a "for" loop
figure;
What I have thus far:
% for_sines.m
% yourname and lab section
fprintf('\n Gretchen Paden, Th 3:00-5:00 \n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
sin_t(1,:) = sin(2*pi*f(1).*t)
t = 0:.01:2.5
f = 6:2:10
% initialize f to its proper values using a "for" loop
for f=0:.01:2.5
disp(f)
end
% initialize t to its proper values using a "for" loop
for t = 0:.01:2.5
disp(f)
end
% calculate the sine and plot using a "for" loop
figure;
for ii
y(ii,;)=sin(2*pi*f(ii).*t);
subplot(3,1,ii),plot(x,y(1,:),'k');ylabel('y');xlabel('x');title(Frequency);grid on
end
Let me know if you can help. Thanks a million!!
shwaip
10 Mar 2009, 6:15pm
comments are in code
% for_sines.m
% yourname and lab section
fprintf('\n Gretchen Paden, Th 3:00-5:00 \n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
% sin_t(1,:) = sin(2*pi*f(1).*t)
% t = 0:.01:2.5
% f = 6:2:10
%you don't need any of these lines, and you're supposed to initialize t, f using a for loop.
%(your method is actually better, but that's not what's assigned).
% initialize f to its proper values using a "for" loop
for f=0:.01:2.5
disp(f)
end
%all you're doing here is displaying numbers. you need to store (6,8,10) into an array, f.
% initialize t to its proper values using a "for" loop
for t = 0:.01:2.5
disp(f)
end
%same as above, but now you want to store the values into an array, t.
% calculate the sine and plot using a "for" loop
%you should do this in 2 steps. first calculate the sin values, and then plot.
%you'll need to loop over f and t.
figure;
for ii
y(ii,;)=sin(2*pi*f(ii).*t);
subplot(3,1,ii)
plot(x,y(1,:),'k');
ylabel('y');
xlabel('x');
title('Frequency');
grid on
end
gap9q3
10 Mar 2009, 6:20pm
THANKS SO MUCH!!! May need more help next week! You are a life saver!!!
gap9q3
10 Mar 2009, 6:21pm
Oh, P.S. GO BARACK!!!!
gap9q3
10 Mar 2009, 6:27pm
I'm still getting and error...
% for_sines.m
% yourname and lab section
fprintf('\n Gretchen Paden, Th 3:00-5:00 \n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
% sin_t(1,:) = sin(2*pi*f(1).*t)
% t = 0:.01:2.5
% f = 6:2:10
% initialize f to its proper values using a "for" loop
for i = 6:2:10
f = i
end
% initialize t to its proper values using a "for" loop
for ii = 0:.01:2.5
t = ii
end
% calculate the sine and plot using a "for" loop
%you should do this in 2 steps. first calculate the sin values, and then plot.
%you'll need to loop over f and t.
figure;
for ii
y(ii,:)=sin(2*pi*f(ii).*t);
subplot(3,1,ii)
plot(x,y(1,:),'k');
ylabel('y');
xlabel('x');
title('Frequency');
grid on
end
shwaip
10 Mar 2009, 6:35pm
1) what error.
2) if you post your code inside paste code here it'll look nicer
gap9q3
10 Mar 2009, 6:40pm
1) what error.
2) if you post your code inside % for_sines.m
% yourname and lab section
fprintf('\n Gretchen Paden, Th 3:00-5:00 \n');
% initialize t and sine to zeros
t=zeros(1,251);
sine=zeros(3,length(t));
% sin_t(1,:) = sin(2*pi*f(1).*t)
% t = 0:.01:2.5
% f = 6:2:10
% initialize f to its proper values using a "for" loop
for i = 6:2:10
f = i
end
% initialize t to its proper values using a "for" loop
for ii = 0:.01:2.5
t = ii
end
% calculate the sine and plot using a "for" loop
%you should do this in 2 steps. first calculate the sin values, and then plot.
%you'll need to loop over f and t.
figure;
for n = 1:3
sine(n,:)=sin(2*pi*f(n).*t);
end
for n
subplot(3,1,n)
plot(t,sine(1,:));
ylabel('sin(t)');
xlabel('t');
title('Frequency');
end it'll look nicer
Error: ??? Index exceeds matrix dimensions.
And the plot is blank
shwaip
10 Mar 2009, 10:57pm
it should give you the line that the error is on.
your loops aren't doing what you think they're doing. you might want to try stepping through them (mentally, on paper, or using matlab debug if you know how) and see if they're actually working...
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.