当前位置:   article > 正文

基于matlab的船体三维模型仿真_三维海底建模matlab

三维海底建模matlab

原理介绍

船体数据来源来自于C++代码。在C++中,将船体数据导入到EXCEL中,然后再MATLAB中调用EXCEL的船体数据,进行三维建模。在建模过程中,因为船体数据不可能每个点的距离都是无限小,所以我们采用了拟合的办法,调用了MATLB的函数  griddata 。对船体数据进行三角形插值拟合。最终得到三维船体图形。如下:

遗传算法代码如下

  1. #include <iostream>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5. #include<fstream>
  6. #define tot 122*18*16
  7. using namespace std;
  8. int main(int argc, const char * argv[])
  9. {
  10. double suf[90100];
  11. double vol[90100];
  12. double fin[122][60];
  13. double fin1[122][60];
  14. double ran1[122][60];
  15. double head[60],head2[60];
  16. int min=99999,flag=0;
  17. ofstream out("grade");
  18. if(!out)
  19. {
  20. cout << "Cannot open grade file.\n";
  21. return 1;
  22. }
  23. for(int t=0;t<90100;t++)
  24. {
  25. suf[t]=0;
  26. vol[t]=0;
  27. }
  28. int i=900;
  29. while(i>=0)
  30. {
  31. double ran[122][60];
  32. double area[60];
  33. for(int t=0;t<60;t++)
  34. {
  35. head[t]=3*0.1*0.1*t*t-32*t*0.1;
  36. head2[t]=0.5*t*t*0.01-8*t*0.1;
  37. }
  38. for(int t=0;t<600;t++)
  39. {
  40. area[t]=0;
  41. }
  42. for(int z=0;z<60;z++)
  43. {
  44. for(int x=1;x<122;x++)
  45. {
  46. if(x<100)
  47. {
  48. srand((unsigned)time(NULL)+x*z*i*7);
  49. ran[x][z]=max((11.22925*pow(x, 0.1)+0.01*(abs(rand()%100)))*(((-z*0.1+12.00)*(double)(z*0.1))/36.00),ran[x-1][z]+(((-z*0.1+12.00)*(double)(z*0.1))/36.00)*0.01*(abs(rand()%100)));
  50. ran1[x][z]=max((14.2171*pow(x, 0.05)+0.01*(abs(rand()%100)))*(((-z*0.1+12.00)*(double)(z*0.1))/36.00),ran[x-1][z]+(((-z*0.1+12.00)*(double)(z*0.1))/36.00)*0.01*(abs(rand()%100)));
  51. }else
  52. {
  53. ran[x][z]=ran[x-1][z];
  54. ran1[x][z]=ran1[x-1][z];
  55. }
  56. // cout<<ran[x][z]<<" ";
  57. }
  58. }
  59. for(int z=0;z<60;z++){
  60. for(int x=0;x<122;x++)
  61. {
  62. area[z]+=4*(ran[x][z]+ran[x+1][z]);
  63. }
  64. //cout<<area[z]<<" ";
  65. }
  66. for(int z=1;z<60;z++)
  67. {
  68. for(int x=1;x<122;x++)
  69. {
  70. double a[5];
  71. double p1=0,p2=0;
  72. a[0]=sqrt(abs((ran[x][z]-ran[x-1][z])*(ran[x][z]-ran[x-1][z])+((122+head[z])/122)*((122+head[z])/122)));
  73. a[1]=sqrt(abs((ran[x][z]-ran[x][z-1])*(ran[x][z]-ran[x][z-1])+1+(((122-x)*(head[z]-head[z-1]))/122)*(((122-x)*(head[z]-head[z-1]))/122)));
  74. a[2]=sqrt(abs((ran[x-1][z]-ran[x][z-1])*(ran[x-1][z]-ran[x][z-1])+1+((((122-x+1)*(head[z-1]-head[z]))/122)+(122+head[z-1])/122)*((((122-x+1)*(head[z-1]-head[z]))/122)+(122+head[z-1])/122)));
  75. a[3]=sqrt(abs((ran[x][z-1]-ran[x-1][z-1])*(ran[x][z-1]-ran[x-1][z-1])+((122+head[z-1])/122)*((122+head[z-1])/122)));
  76. a[4]=sqrt(abs((ran[x-1][z]-ran[x-1][z-1])*(ran[x-1][z]-ran[x-1][z-1])+(((122-x+1)*(head[z]-head[z-1]))/122)*(((122-x+1)*(head[z]-head[z-1]))/122)));
  77. p1=(a[0]+a[1]+a[2])/2;
  78. p2=(a[2]+a[3]+a[4])/2;
  79. suf[i]+=sqrt(abs(p1*(p1-a[0])*(p1-a[1])*(p1-a[2])));
  80. suf[i]+=sqrt(abs(p2*(p2-a[2])*(p2-a[3])*(p2-a[4])));
  81. }
  82. }
  83. vol[i]=(area[0]+area[5])*0.5+area[1]+area[2]+area[3]+area[4];
  84. if(suf[i]<min)
  85. {
  86. min=suf[i];
  87. flag=i;
  88. for(int t=0;t<122;t++)
  89. {
  90. for(int q=0;q<60;q++)
  91. {
  92. fin[t][q]=ran[t][q];
  93. fin1[t][q]=ran1[t][q];
  94. }
  95. }
  96. }
  97. //cout<<suf[i]<<" ";
  98. i--;
  99. }
  100. cout<<flag<<" "<<min<<" "<<vol[flag]<<" "<<suf[flag]<<endl;
  101. for(int t=0;t<60;t++)
  102. {
  103. for(int i=0;i<122;i++)
  104. {
  105. out<<0.5*((double)i+((double)(122-i)*head[t]/(double)122))<<" "<<fin[i][t]<<" "<<(double)t<<endl;
  106. out<<0.5*((double)i+((double)(122-i)*head[t]/(double)122))<<" "<<-fin[i][t]<<" "<<(double)t<<endl;
  107. out<<0.5*((double)244-(double)i-((double)(122-i)*head2[t]/(double)122))<<" "<<fin1[i][t]<<" "<<(double)t<<endl;
  108. out<<0.5*((double)244-(double)i-((double)(122-i)*head2[t]/(double)122))<<" "<<-fin1[i][t]<<" "<<(double)t<<endl;
  109. }
  110. cout<<endl;
  111. }
  112. out.close();
  113. }

matlab仿真代码如下:

  1. function createfigure(xdata1, ydata1, zdata1, cdata1, X1, Y1, Z1)%CREATEFIGURE(xdata1, ydata1, zdata1, cdata1, X1, Y1, Z1)%  XDATA1:  surface xdata%  YDATA1:  surface ydata%  ZDATA1:  surface zdata%  CDATA1:  surface cdata%  X1:  x 数据的向量%  Y1:  y 数据的向量%  Z1:  z 数据的向量%  由 MATLAB 于 31-Oct-2020 15:47:44 自动生成% 创建 figurefigure1 = figure;% 创建 axesaxes1 = axes(&apos;Parent&apos;,figure1);hold(axes1,&apos;on&apos;);% 创建 meshmesh(xdata1,ydata1,zdata1,cdata1,&apos;Parent&apos;,axes1);% 创建 plot3plot3(X1,Y1,Z1,&apos;MarkerSize&apos;,15);% 创建 titletitle(&apos;船体模型&apos;);view(axes1,[-37.5 30]);grid(axes1,&apos;on&apos;);axis(axes1,&apos;tight&apos;);hold(axes1,&apos;off&apos;);% 设置其余坐标区属性set(axes1,&apos;DataAspectRatio&apos;,[1 1 1]);
  2. clc;x=Column1(:,1);%导入数据y=Column2(:,1);%导入数据z=Column3(:,1);%导入数据xlin = linspace(min(x),max(x),1000);%对X插值ylin = linspace(min(y),max(y),1000);%对Y插值[X,Y] = meshgrid(xlin,ylin);%对X,Y进行曲面拟合Z = griddata(x,y,z,X,Y,&apos;linear&apos;);%对Z进行拟合mesh(X,Y,Z) %interpolatedaxis equal;axis tight; hold on%设置坐标轴plot3(x,y,z,&apos;MarkerSize&apos;,15) %nonuniform%进行绘制三维图形title(&apos;船体模型&apos;);% plot3(x,y,z)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/314761?site
推荐阅读
相关标签
  

闽ICP备14008679号