extracting arrays from a taylor series and plotting
Ältere Kommentare anzeigen
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
Antworten (3)
per isakson
am 11 Feb. 2015
Bearbeitet: per isakson
am 11 Feb. 2015
1 Stimme
Hints:
- aprrox50   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   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 Kommentare
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
Bearbeitet: per isakson
am 11 Feb. 2015
I've edited my answer
- "have to write a script file"   better not argue with that
- "I have to define the T function before the loop correct"   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
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
Bearbeitet: per isakson
am 11 Feb. 2015
Hint
>> whos ...
Name Size Bytes Class Attributes
do 1x720 5760 double
approx5 1x51 408 double
Length differs. Reconsider T = zeros(51);
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
Bearbeitet: per isakson
am 11 Feb. 2015
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.
Max
am 11 Feb. 2015
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
Bearbeitet: per isakson
am 11 Feb. 2015
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.
Max
am 11 Feb. 2015
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
This is the starting point

Note summation from k=0
Max
am 11 Feb. 2015
Max
am 11 Feb. 2015
per isakson
am 11 Feb. 2015
Yes, Matlab's indexing is "one-based", but in one way or other you have to include the first term in the series!
Max
am 11 Feb. 2015
Roger Stafford
am 11 Feb. 2015
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 Kommentare
Max
am 11 Feb. 2015
Roger Stafford
am 11 Feb. 2015
Bearbeitet: Roger Stafford
am 11 Feb. 2015
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?
Max
am 11 Feb. 2015
Max
am 11 Feb. 2015
daniel
am 11 Feb. 2015
0 Stimmen
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 Kommentar
Max
am 11 Feb. 2015
Kategorien
Mehr zu Sparse Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!