Help with plotting

sandbananasandbanana Here
edited September 2010 in Science & Tech
I have a code that asks for a value of x and then the number of terms about zero e^x should be expanded using a Taylor Series. That one seems to work fine.

The next one takes x from -10 to 10 with a 10 term Taylor Series expansion. I need to be about to plot e^x vs x for each one of these values.

Here is the code I have:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This will compute the Taylor Series expansion of e^x for a user defined
%x value for the number of terms required by the user.  It will do this about the point
%a=0.  The result of the
%nth term will be compared to the computer generated value of e^x

clear %clears previous values 

x = input ('Enter a value for x:'); % user input of which value to use for x
i = input ('Enter the non-zero number of terms for this Taylor Series expansion:'); %user input of number of terms to use

% g_n: the nth term in Taylor Series

k=1;    % initialize k

g_n=x^(k-1)/factorial(k-1);   % begin with the first term

g=g_n;

while  k<i; %="" let="" index="" increase="" until="" number="" of="" desired="" terms="" reached="">
 
    k=k+1;  % increase index by 1
    g_n=x^(k-1)/factorial(k-1);
    g=g+g_n; %add the next term in the series   
end

display(g)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This will compute the Taylor Series expansion about the point zero for x
%values -10 to 10 using a 10 term expansion

% g_n: the nth term in Taylor Series

clear %clears previous values

for x=-10:10 %describes the range of values for x

k=1;    % initialize k
i=10;   % number of terms used

g_n=x^(k-1)/factorial(k-1);   % begin with the first term

g=g_n;

while  k<i; %="" let="" index="" increase="" until="" number="" of="" desired="" terms="" reached="">
 
    k=k+1;  % increase index by 1
    g_n=x^(k-1)/factorial(k-1);
    g=g+g_n; %add the next term in the series   
end
display(x)
display(g)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This will now calculate the actual values for e^x using a range of x=-10
%to x=10

clear % clears previous values

for x=-10:10
    y=exp(x);
    
    display(x)
    display(y)
    plot(y,x)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
</i;></i;>

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2010
    your for loop:
    while k
    ...
    
    will run forever (or at least until k is zero)

    (there's your problem)
Sign In or Register to comment.