extracting arrays from a taylor series and plotting

I have been working on a script that calculates a Taylor series without using the built-in function.
I am having a lot of trouble extracting the proper array of numbers for the three iteration values I need to plot together with the error (I need to plot N = 2 5 & 50 with the exact function of 5sin(3x)).
This is what I have:
clear;clc
n =[2 5 50];
do=linspace(-2*pi,2*pi,720);
T = zeros(51);
for i =1:720
for k=1:1:50;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
approx2 = T(:,2);
approx5 = T(:,5);
aprrox50 = T(:,50);
exact = 5*sin(3*do);
plot(do,exact, '-r')
hold on
ez1=ezplot('approx2');
ez2=ezplot('approx5');
ez3=ezplot('approx50');
legend('5sin(3x)','T2','T5','T50')
set(ez1, 'color', [0 1 0])
set(ez2, 'color', [0 0 1])
set(ez3, 'color', [1 0 1])
title('Graph of 5sin(3x) and taylor expansions T2, T5 and T50')
I cannot get it to graph properly though......
I have looked through the matlab help file and searched through google.
I am thinking it might have to do with not defning the Taylor iterations properly.
This is a homework assignment so I AM NOT LOOKING FOR SOMEONE TO DO THIS FOR ME.
Any hints or tips to put me in the right direction will be greatly appreciated!
Thank You

回答(3 个)

per isakson
per isakson 2015-2-11
编辑:per isakson 2015-2-11
Hints:
  • aprrox50 &nbsp is a typo
  • read the doc on ezplot, note that approx.. are double vectors
  • use plot to plot approx..
  • I find it easier to work with functions than with scripts when it comes to debugging - it used to be anyhow.
  • try &nbsp figure, imagesc(T), colorbar
  • ALWAYSONTOP, by Elmar Tarajan makes it possible to see the effect on the diagram of each line of code; step through the code and watch - or dock the figure

16 个评论

The problem states that i have to write a script file.
Zeros wise, I have to define the T function before the loop correct?
So i tried just T = 0, bu then I receive an error saying "index out of bounds because numel(T)=1
thats why I defined it as the 51x51 zero matrix
I've edited my answer
  • "have to write a script file" &nbsp better not argue with that
  • "I have to define the T function before the loop correct" &nbsp not exactly have to, but it critical for performance. zeros(51,51) communicates intent better; I was fooled by T(ii), which you use in the assignment
clear;clc
n =[2 5 50];
do=linspace(-2*pi,2*pi,720);
T = zeros(51);
for i =1:720
for k=1:1:50;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:720
for k=1:1:5;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:720
for k=1:1:2;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
approx2 = T(2,:);
approx5 = T(5,:);
aprrox6 = T(50,:);
exact = 5*sin(3*do);
plot(do,exact, '-r')
hold on
ez1=plot(do,approx2, '-m');
ez2=plot(do,approx5, '-g');
ez3=plot(do,approx6, '-b');
legend('5sin(3x)','T2','T5','T50')
title('Graph of 5sin(3x) and taylor expansions T2, T5 and T50')
When I have the 3 separate loops it gives me better values,but still having trouble plotting them all together.
Hint
>> whos ...
Name Size Bytes Class Attributes
do 1x720 5760 double
approx5 1x51 408 double
Length differs. Reconsider T = zeros(51);
T = zeros(720) correct?
I do that and it makes them all the same dimension.
Problem I am having though is the approx2, approx5, and approx6 arrays are just 1 value followed by a bunch of zeros......
hmm do i need to store each iteration at the end of the loop before the end?
T = zeros(720);
is not compatible with
approx2 = T(2,:);
approx5 = T(5,:);
aprrox6 = T(50,:);
...
T(i)=T(i) ...
T is not needed, it just make the code more complicated. Allocating 720 rows and using 3. Why not
approx2 = zeros(size(do));
ect.
and use approx2 in the loop.
So ive gotten this far:
clear;clc
n =[2 5 50];
do=linspace(-2*pi,2*pi,720);
T = zeros(720);
for i =1:720
for k=1:1:50;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:720
for k=1:1:5;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:720
for k=1:1:2;
ns=2*k+1;
T(i)=T(i)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
approx2 = T(2,:);
approx5 = T(5,:);
approx6 = T(50,:);
exact = 5*sin(3*do);
plot(do,exact, '-r')
hold on
plot(do,approx2, '-m');
plot(do,approx5, '-g');
plot(do,approx6, '-b');
legend('5sin(3x)','T2','T5','T50')
title('Graph of 5sin(3x) and taylor expansions T2, T5 and T50')
Its almost right except it is figuring the approx2 - approx6 values as points, so it just ends up graphing a line........
even stranger is it graphs the exact value as being equal to (-5,5)
Thats a good idea Per Isakson, il ltry that out
I would say: Take three steps away from the keyboard and write done some pseudo code on a piece of paper.
And
do=linspace(-2*pi,2*pi,720);
You chosen the name do. I would have chosen x.
I tried this:
clear;clc
n =[2 5 50];
do=linspace(-2*pi,2*pi,100);
approx2 = zeros(size(do));
approx5 = zeros(size(do));
approx6 = zeros(size(do));
for i =1:100
for k=1:1:50;
ns=2*k+1;
approx6 = approx6 +5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:100
for k=1:1:5;
ns=2*k+1;
approx5 = approx5 +5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
for i =1:100
for k=1:1:2;
ns=2*k+1;
approx2 = approx2 +5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
exact = 5*sin(3*do);
plot(do,exact, '-r')
hold on
plot(do,approx2, '-m');
plot(do,approx5, '-g');
plot(do,approx6, '-b');
legend('5sin(3x)','T2','T5','T50')
title('Graph of 5sin(3x) and taylor expansions T2, T5 and T50')
It doesnt work because I need to define in the taylor series function that it is added to the approx before it not approx6 = approx6 + 5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
im trying to think of a way that i could define it to have the first term be the preceding factor....
Im getting closer:
clear;clc
n =[2 5 50];
x=linspace(-2*pi,2*pi,100);
approx2 = zeros(size(x));
approx5 = zeros(size(x));
approx6 = zeros(size(x));
for i =1:100
for k=1:1:50;
ns=2*k+1;
approx6(i) = approx6(i) +5*(((-1)^k)*(3*x(i))^(ns))/factorial(ns);
end
end
for i =1:100
for k=1:1:5;
ns=2*k+1;
approx5(i) = approx5(i) +5*(((-1)^k)*(3*x(i))^(ns))/factorial(ns);
end
end
for i =1:100
for k=1:1:2;
ns=2*k+1;
approx2(i) = approx2(i) +5*(((-1)^k)*(3*x(i))^(ns))/factorial(ns);
end
end
exact = 5*sin(3*x);
plot(x,exact, '-r')
hold on
plot(x,approx2, '-m');
plot(x,approx5, '-g');
plot(x,approx6, '-b');
legend('5sin(3x)','T2','T5','T50')
title('Graph of 5sin(3x) and taylor expansions T2, T5 and T50')
The script still gets weird with the graphing portion though
This is the starting point
Note summation from k=0
I got that but matlab cannot start at 0 i though
hence the reason why k was always definded as k = 1:n
Yes, Matlab's indexing is "one-based", but in one way or other you have to include the first term in the series!
well the first term is always 15x
so would defining the series as
(for example)
approx2(i) = 15*x + 5*(((-1)^k)*(3*x(i))^(ns))/factorial(ns);
i want to say that takes care of the 0 term

请先登录,再进行评论。

It looks as though your T is incorrectly computed. For each of three different counts, n = 2, 5, and 50, you need to compute n terms of the Taylor series for each of 720 different values of the angle. That would imply three, not two, nested for-loops or their equivalent, and the resulting T should be two-dimensional, not one-dimensional.
for n = [2,5,50]
for i = 1:720
for k = 1:n
etc.
(Of course, with the appropriate use of matlab's 'sum' function you could eliminate at least one of these loops.)

4 个评论

I am thinking that multiple loops is the answer, unfortunately the problem I am working on specifically states you cannot use any of the built in sum features....
I really should have written the loops in my suggestion this way:
T = zeros(720,3);
n = [2,5,50];
for j = 1:3
for i = 1:720
for k = 1:n(j)
T(i,j) = T(i,j) + ....
.......
approx2 = T(:,1);
approx5 = T(:,2);
approx50 = T(:,3);
Does that help you any?
Yes this does help, but for the three loops would i define the j values as
j = 1:2
j = 1:5
J = 1:50
I know they would go at the begging of each loop for that specific number of iterations.
I tried this:
T = zeros(720,3);
n = [2 5 50];
do=linspace(-2*pi,2*pi,720);
for j = 1:2
for i = 1:720
for k = 1:n(j)
ns=2*k+1;
T(i,j)=T(i,j)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
end
for j = 1:5
for i = 1:720
for k = 1:n(j)
ns=2*k+1;
T(i,j)=T(i,j)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
end
for j = 1:50
for i = 1:720
for k = 1:n(j)
ns=2*k+1;
T(i,j)=T(i,j)+5*(((-1)^k)*(3*do(i))^(ns))/factorial(ns);
end
end
end
approx2 = T(:,1);
approx5 = T(:,5);
approx50 = T(:,50);
But matlab complains about n(4) being out of bonds because numel(n)=3
which makes no sense to me because its defined at the begging of the script that n = [2 5 50]...

请先登录,再进行评论。

The first problem I see is that "exact" is a 1x720 vector and your approximations are 51x1 vectors; my first suggestion would be to get them all in the same domain. Once they all span the same domain and are of the same size, your plot should look better.

1 个评论

Thats an excellent point, I could just transpose the approx values but then it still wouldnt match size wise

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Sparse Matrices 的更多信息

提问:

Max
2015-2-11

评论:

Max
2015-2-11

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by