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

## 0 Comments

Sign in to comment.