Advertisement
Guest User

Untitled

a guest
May 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.92 KB | None | 0 0
  1. clc;
  2. fprintf('1. Rysowanie wykresu pochodnej funkcji f(x) na przedziale [x1,x2]\n');
  3. fprintf('2. Wyznaczanie pochodnej funkcji f(x) w punkcie x0\n');
  4. fprintf('3. Rysowanie wykresow pochodnych czastkowych funkcji f(x,y) na przedziale [x1,x2]x[y1,y2]\n');
  5. fprintf('4. Wyznaczanie gradientu funkcji f(x,y) w punkcie (x0,y0)\n');
  6. fprintf('5. Wyznaczanie gradientu funkcji f(x,y,z) w punkcie (x0,y0,z0)\n\n');
  7. k=input('Wybierz (1-5): ');
  8. h=0.001; % przyrost w def. pochodnej dla case 2,4,5
  9. switch k
  10.   case 1
  11.     x1=input('x1=');
  12.     x2=input('x2=');
  13.     x=linspace(x1,x2);
  14.     f=input('f(x)=');
  15.     if (length(f)==1) % jesli f jest funkcja stala
  16.       f=f*ones(1,100);
  17.     end
  18.     Df=diff(f)./diff(x);
  19.     plot(x(1:99),[f(1:99);Df]);
  20.     xlabel('x');
  21.     ylabel('y');
  22.     legend('f(x)','Df(x)');
  23.   case 2
  24.     x0=input('x0=');
  25.     x=[x0-h/2,x0+h/2];
  26.     f=input('f(x)=');
  27.     if (length(f)==1) % jesli f jest funkcja stala
  28.       f=f*ones(1,2);
  29.     end
  30.     Df=diff(f)/h;
  31.     fprintf('Df(%f)=%f\n',x0,Df);
  32.   case 3
  33.     x1=input('x1=');
  34.     x2=input('x2=');
  35.     y1=input('y1=');
  36.     y2=input('y2=');
  37.     x=linspace(x1,x2);
  38.     y=linspace(y1,y2);
  39.     [x,y]=meshgrid(x,y);
  40.     f=input('f(x,y)=');
  41.     if (length(f)==1) % jesli f jest funkcja stala
  42.       f=f*ones(100,100);
  43.     end
  44.     Dfx=transpose(diff(transpose(f))./diff(transpose(x)));
  45.     Dfy=diff(f)./diff(y);
  46.     subplot(1,3,1); % wykres f(x,y)
  47.     plot3(x,y,f);
  48.     title('f(x,y)');
  49.     xlabel('x');
  50.     ylabel('y');
  51.     zlabel('z');
  52.     subplot(1,3,2); % wykres df/dx(x,y)
  53.     plot3(x(:,1:99),y(:,1:99),Dfx);
  54.     title('df/dx(x,y)');
  55.     xlabel('x');
  56.     ylabel('y');
  57.     zlabel('z');
  58.     subplot(1,3,3); % wykres df/dy(x,y)
  59.     plot3(x(1:99,:),y(1:99,:),Dfy);
  60.     title('df/dy(x,y)');
  61.     xlabel('x');
  62.     ylabel('y');
  63.     zlabel('z');
  64.   case 4
  65.     x0=input('x0=');
  66.     y0=input('y0=');
  67.     x=[x0-h/2,x0,x0+h/2];
  68.     y=[y0-h/2,y0,y0+h/2];
  69.     [x,y]=meshgrid(x,y);
  70.     f=input('f(x,y)=');
  71.     if (length(f)==1) % jesli f jest funkcja stala
  72.       f=f*ones(3,3);
  73.     end
  74.     Dfx=(f(2,3)-f(2,1))/h; % tzn. (f(x0+h/2,y0)-f(x0-h/2,y0))/h
  75.     Dfy=(f(3,2)-f(1,2))/h; % tzn. (f(x0,y0+h/2)-f(x0,y0-h/2))/h
  76.     Df=[Dfx,Dfy];
  77.     fprintf('Df(%f,%f)=(%f,%f)\n',x0,y0,Dfx,Dfy);
  78.   case 5
  79.     x0=input('x0=');
  80.     y0=input('y0=');
  81.     z0=input('z0=');
  82.     x=[x0-h/2,x0,x0+h/2];
  83.     y=[y0-h/2,y0,y0+h/2];
  84.     z=[z0-h/2,z0,z0+h/2];
  85.     [x,y,z]=meshgrid(x,y,z);
  86.     f=input('f(x,y,z)=');
  87.     if (length(f)==1) % jesli f jest funkcja stala
  88.       f=f*ones(3,3,3);
  89.     end
  90.     Dfx=(f(2,3,2)-f(2,1,2))/h; % tzn. (f(x0+h/2,y0,z0)-f(x0-h/2,y0,z0))/h
  91.     Dfy=(f(3,2,2)-f(1,2,2))/h; % tzn. (f(x0,y0+h/2,z0)-f(x0,y0-h/2,z0))/h
  92.     Dfz=(f(2,2,3)-f(2,2,1))/h; % tzn. (f(x0,y0,z0+h/2)-f(x0,y0,z0-h/2))/h
  93.     Df=[Dfx,Dfy,Dfz];
  94.     fprintf('Df(%f,%f,%f)=(%f,%f,%f)\n',x0,y0,z0,Dfx,Dfy,Dfz);
  95.   otherwise
  96.     fprintf('Blednie wybrane polecenie\n');
  97. end % koniec switcha
  98.  
  99. % eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement