To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
gap9q3
Getting settled in
gap9q3
8 Posts

Matlab Help - Taylor Series

I am studying and I cannot figure out how to do this practice problem... Can ANYONE help me please! Thanks!


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
elaborate bot
shwaip
5,730 Posts
post the code of any attempt you've made, and we'll help you out.
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
gap9q3
Getting settled in
gap9q3
8 Posts
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
elaborate bot
shwaip
5,730 Posts
close.

matlab uses 1-based indexing, so the first element of the array is x(1).

i'd do something like this:
Code:
k=5
x=0.5
est=0;
for n=0:k
  est = est + x^n;
end
gap9q3
Getting settled in
gap9q3
8 Posts
close.

matlab uses 1-based indexing, so the first element of the array is x(1).

i'd do something like this:
Code:
k=5
x=0.5
est=0;
for n=0:k
  est = est + x^n;
end

THANKS!! I think I got it!

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.
Code:
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:
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
% 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
elaborate bot
shwaip
5,730 Posts
comments are in code

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
Getting settled in
gap9q3
8 Posts
THANKS SO MUCH!!! May need more help next week! You are a life saver!!!
gap9q3
Getting settled in
gap9q3
8 Posts
Oh, P.S. GO BARACK!!!!
gap9q3
Getting settled in
gap9q3
8 Posts
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
elaborate bot
shwaip
5,730 Posts
1) what error.

2) if you post your code inside
PHP Code:
[code]paste code here[/code
it'll look nicer
gap9q3
Getting settled in
gap9q3
8 Posts
1) what error.

2) if you post your code inside
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

% 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
elaborate bot
shwaip
5,730 Posts
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...
Similar Threads
Thread Thread Starter Forum Replies Last Post
MATLAB - Please Help!!!!!!!!!!! mypeace Matlab Help 2 15 Oct 2008 4:59pm
Trying to learn matlab (Taylor Series) kalen Matlab Help 4 25 Oct 2007 2:46am
Help with MATLAB GUI pbutter Matlab Help 0 28 Sep 2007 5:44pm
Matlab problem - Power Series implementation skillz Matlab Help 15 27 Apr 2007 10:49pm
MatLab help - Taylor Series julianne Matlab Help 2 31 Jan 2007 4:15pm

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 2:05pm (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.