[已答复] 绘制函数图形

[复制链接]
夕木小径 发表于 2021-5-6 21:52:30
如果矩阵中的某一个元素为函数,请教,如何才能画出该元素的函数图形;例如画出如下程序中矩阵M(2,4)的三维图形
clc
close all
clear
%%
syms Psi delt

sigma1=30;
sigma2=30;
theta1=30;
theta2=30;

N=cosd(2*Psi);
Sc=sind(2*Psi)*cosd(delt);
Ss=sind(2*Psi)*sind(delt);

A1=1-(1-cosd(sigma1))*sind(2*theta1)*sind(2*theta1);
A2=1-(1-cosd(sigma2))*sind(2*theta2)*sind(2*theta2);
B1=(1-cosd(sigma1))*sind(2*theta1)*cosd(2*theta1);
B2=(1-cosd(sigma2))*sind(2*theta2)*cosd(2*theta2);
C1=sind(sigma1)*sind(2*theta1);
C2=sind(sigma2)*sind(2*theta2);
D1=1-(1-cosd(sigma1))*cosd(2*theta1)*cosd(2*theta1);
D2=1-(1-cosd(sigma2))*cosd(2*theta2)*cosd(2*theta2);
E1=sind(sigma1)*cosd(2*theta1);
E2=sind(sigma2)*cosd(2*theta2);
F1=cosd(sigma1);
F2=cosd(sigma2);

W2=[1 0 0 0;0 A2 B2 -C2;0 B2 D2 F2;0 C2 -E2 F2];
S=[1 -N 0 0;-N 1 0 0;0 0 Sc Ss;0 0 -Ss Sc];
W1=[1 0 0 0;0 A1 B1 -C1;0 B1 D1 F1;0 C1 -E1 F1];
M=W2*S*W1;

[Psi,delt] = meshgrid(-2*pi:2*pi);
mesh(Psi,delt,M(2,4))
set(gca,'xlim',[-2*pi,2*pi]);%设置x轴坐标范围
set(gca,'xtick',-2*pi:pi/2:2*pi)%设置x轴坐标间隔
set(gca,'XTickLabel',{'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'});%\pi=π
set(gca,'ylim',[-2*pi,2*pi]);%设置y轴坐标范围
set(gca,'ytick',-2*pi:pi/2:2*pi);%设置y轴坐标间隔
set(gca,'YTickLabel',{'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'});%\pi=π
xlabel('Ψ'),ylabel('Δ'),zlabel('');

运行时提示,z必须为矩阵,不能是标量或向量;请教解决该问题的方法

2 条回复


20141303 发表于 2021-5-7 08:23:19
仅供参考
  1. clc
  2. close all
  3. clear
  4. %%
  5. syms Psi delt

  6. sigma1=30;
  7. sigma2=30;
  8. theta1=30;
  9. theta2=30;

  10. N=cosd(2*Psi);
  11. Sc=sind(2*Psi)*cosd(delt);
  12. Ss=sind(2*Psi)*sind(delt);

  13. A1=1-(1-cosd(sigma1))*sind(2*theta1)*sind(2*theta1);
  14. A2=1-(1-cosd(sigma2))*sind(2*theta2)*sind(2*theta2);
  15. B1=(1-cosd(sigma1))*sind(2*theta1)*cosd(2*theta1);
  16. B2=(1-cosd(sigma2))*sind(2*theta2)*cosd(2*theta2);
  17. C1=sind(sigma1)*sind(2*theta1);
  18. C2=sind(sigma2)*sind(2*theta2);
  19. D1=1-(1-cosd(sigma1))*cosd(2*theta1)*cosd(2*theta1);
  20. D2=1-(1-cosd(sigma2))*cosd(2*theta2)*cosd(2*theta2);
  21. E1=sind(sigma1)*cosd(2*theta1);
  22. E2=sind(sigma2)*cosd(2*theta2);
  23. F1=cosd(sigma1);
  24. F2=cosd(sigma2);

  25. W2=[1 0 0 0;0 A2 B2 -C2;0 B2 D2 F2;0 C2 -E2 F2];
  26. S=[1 -N 0 0;-N 1 0 0;0 0 Sc Ss;0 0 -Ss Sc];
  27. W1=[1 0 0 0;0 A1 B1 -C1;0 B1 D1 F1;0 C1 -E1 F1];
  28. M=W2*S*W1;

  29. [Psi1,delt1] = meshgrid(-2*pi:2*pi);
  30. for i=1:size(Psi1,1)
  31.     for j=1:size(Psi1,2)
  32.         mid(i,j)=double(subs(M(2,4),[Psi,delt],[Psi1(i,j),delt1(i,j)]));
  33.     end
  34. end

  35. mesh(Psi1,delt1,mid)
  36. set(gca,'xlim',[-2*pi,2*pi]);%设置x轴坐标范围
  37. set(gca,'xtick',-2*pi:pi/2:2*pi)%设置x轴坐标间隔
  38. set(gca,'XTickLabel',{'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'});%\pi=π
  39. set(gca,'ylim',[-2*pi,2*pi]);%设置y轴坐标范围
  40. set(gca,'ytick',-2*pi:pi/2:2*pi);%设置y轴坐标间隔
  41. set(gca,'YTickLabel',{'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'});%\pi=π
  42. xlabel('Ψ'),ylabel('Δ'),zlabel('');
复制代码

夕木小径 发表于 2021-5-11 09:17:07

已解决,多谢前辈指教
您需要登录后才可以回帖 登录 | 注册

本版积分规则

相关帖子
热门教程
站长推荐
快速回复 返回顶部 返回列表