-2

Matlab 中的矩阵乘法又遇到了一个问题。我必须为给定函数绘制泰勒多项式。这个问题与我之前的问题相似(但这次,函数是f: R^2 -> R^3),我不知道如何制作矩阵以使其工作......

function example
   clf;
   M = 40; 
   N = 20; 

   % domain of f(x)
   x1 = linspace(0,2*pi,M).'*ones(1,N); 
   x2 = ones(M,1)*linspace(0,2*pi,N);

   [y1,y2,y3] = F(x1,x2); 
   mesh(y1,y2,y3,...
     'facecolor','w',...
     'edgecolor','k');
   axis equal; 
   axis vis3d; 
   axis manual; 
   hold on

   % point for our Taylor polynom
   xx1 = 3; 
   xx2 = 0.5; 
   [yy1,yy2,yy3] = F(xx1,xx2); 

   % plots one discrete point
   plot3(yy1,yy2,yy3,'ro'); 

   [y1,y2,y3] = T1(xx1,xx2,x1,x2); 
   mesh(y1,y2,y3,...
     'facecolor','w',...
     'edgecolor','g');


% given function
function [y1,y2,y3] = F(x1,x2)
   % constants
   R=2; r=1; 

   y1 = (R+r*cos(x2)).*cos(x1);
   y2 = (R+r*cos(x2)).*sin(x1);
   y3 = r*sin(x2);

function [y1,y2,y3] = T1(xx1,xx2,x1,x2)
   dy = [
     -(R + r*cos(xx2))*sin(xx1) -r*cos(xx1)*sin(xx2) 
      (R + r*cos(xx2))*cos(xx1) -r*sin(xx1)*sin(xx2) 
                              0  r*cos(xx2)          ];
   y = F(xx1, xx2) + dy.*[x1-xx1; x2-xx2];

function [y1,y2,y3] = T2(xx1,xx2,x1,x2)
% ?

我知道我的代码充满了错误(我只需要修复我的 T1 函数)。dy表示雅可比矩阵(总推导f(x)- 我希望我做对了......)。我不确定Hessian矩阵的T2外观如何,希望我能弄清楚,我只是迷失在Matlab中......

编辑:我试图改进我的格式 - 这是我的雅可比矩阵

[-(R + r*cos(xx2))*sin(xx1), -r*cos(xx1)*sin(xx2)...
  (R + r*cos(xx2))*cos(xx1), -r*sin(xx1)*sin(xx2)...
  0,                          r*cos(xx2)];
4

1 回答 1

1
function [y1,y2,y3]=T1(xx1,xx2,x1,x2)
    R=2; r=1;
    %derivatives
    y1dx1 = -(R + r * cos(xx2)) * sin(xx1);
    y1dx2 = -r * cos(xx1) * sin(xx2);
    y2dx1 = (R + r * cos(xx2)) * cos(xx1);
    y2dx2 = -r * sin(xx1) * sin(xx2);
    y3dx1 = 0;
    y3dx2 = r * cos(xx2);
    %T1
    [f1, f2, f3] = F(xx1, xx2);
    y1 = f1 + y1dx1*(x1-xx1) + y1dx2*(x2-xx2);
    y2 = f2 + y2dx1*(x1-xx1) + y2dx2*(x2-xx2);
    y3 = f3 + y3dx1*(x1-xx1) + y3dx2*(x2-xx2);
于 2012-11-17T13:46:05.407 回答