Вопрос

I have one very simple question. We are given a function f: R -> R^2 that looks like this: f(x) = [cos(x) ; sin(2*x)]

We must display the function together with first and second Taylor polynom in one graph. I have computed the polynoms, but I have problem with getting it work inside matlab (matrix dimension problem). Perhaps you could help me find the mistake, here´s my code:

function display
clf;
x=linspace(0,2*pi,1000);
y=F(x); plot(y(1,:),y(2,:),'k'); 
axis equal; axis manual; hold on
xx=1;
yy=F(xx); plot(yy(1,:),yy(2,:),'ro'); 
y=T1(xx,x); plot(y(1,:),y(2,:),'g'); 
%y=T2(xx,x); plot(y(1,:),y(2,:),'b'); 
return

This display function is correct, problem is with functions T1 and T2:

function y=F(x)
y=[cos(x);sin(2*x)];

function y=T1(xx,x)
dy=[-sin(xx);  2*cos(2*xx)]; % 1st derivative at xx
y=F(xx) + dy.*(x-xx);

function y=T2(xx,x)
ddy=[-cos(xx); -4*sin(2*x)]; % 2nd derivative at xx
y=T1(xx,x)+ ((x-xx).*ddy.*(x-xx))/2
Это было полезно?

Решение

You are getting a problem because you are trying to multiply matrices of different sizes.

This is partly because you have set xx to the single value 1. If you change this to a matrix of the same size as x you this should work with with the ones, e.g.

xx=ones(size(x))

Also, in your T1 and T2 functions, your dy matrix is 2 rows by 1000 columns. You must multiply by a matrix of the same dimensions - you could do this for example:

y=F(xx) + dy.*[x-xx;x-xx]; in T1 and:

y=T1(xx,x)+ (ddy.*[x-xx;x-xx].*[x-xx;x-xx])/2; in T2

There is also a slight typo on one line in T2 (an x should be an xx):

ddy=[-cos(xx); -4*sin(2*xx)]; % 2nd derivative at xx

Finally, I found the use of xx as a variable slightly confusing. I would have chosen a as used here:

[http://en.wikipedia.org/wiki/Taylor_series]

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top