赞
踩
原理介绍
船体数据来源来自于C++代码。在C++中,将船体数据导入到EXCEL中,然后再MATLAB中调用EXCEL的船体数据,进行三维建模。在建模过程中,因为船体数据不可能每个点的距离都是无限小,所以我们采用了拟合的办法,调用了MATLB的函数 griddata 。对船体数据进行三角形插值拟合。最终得到三维船体图形。如下:
遗传算法代码如下
- #include <iostream>
- #include<math.h>
- #include<time.h>
- #include<stdlib.h>
- #include<fstream>
- #define tot 122*18*16
- using namespace std;
- int main(int argc, const char * argv[])
- {
- double suf[90100];
- double vol[90100];
- double fin[122][60];
- double fin1[122][60];
- double ran1[122][60];
- double head[60],head2[60];
- int min=99999,flag=0;
- ofstream out("grade");
- if(!out)
- {
- cout << "Cannot open grade file.\n";
- return 1;
- }
- for(int t=0;t<90100;t++)
- {
- suf[t]=0;
- vol[t]=0;
- }
- int i=900;
- while(i>=0)
- {
- double ran[122][60];
- double area[60];
-
- for(int t=0;t<60;t++)
- {
- head[t]=3*0.1*0.1*t*t-32*t*0.1;
- head2[t]=0.5*t*t*0.01-8*t*0.1;
- }
- for(int t=0;t<600;t++)
- {
- area[t]=0;
- }
- for(int z=0;z<60;z++)
- {
- for(int x=1;x<122;x++)
- {
- if(x<100)
- {
-
-
- srand((unsigned)time(NULL)+x*z*i*7);
- 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)));
- 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)));
- }else
- {
- ran[x][z]=ran[x-1][z];
- ran1[x][z]=ran1[x-1][z];
- }
- // cout<<ran[x][z]<<" ";
- }
- }
- for(int z=0;z<60;z++){
- for(int x=0;x<122;x++)
- {
- area[z]+=4*(ran[x][z]+ran[x+1][z]);
- }
- //cout<<area[z]<<" ";
- }
- for(int z=1;z<60;z++)
- {
- for(int x=1;x<122;x++)
- {
- double a[5];
- double p1=0,p2=0;
- 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)));
- 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)));
- 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)));
- 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)));
- 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)));
- p1=(a[0]+a[1]+a[2])/2;
- p2=(a[2]+a[3]+a[4])/2;
- suf[i]+=sqrt(abs(p1*(p1-a[0])*(p1-a[1])*(p1-a[2])));
- suf[i]+=sqrt(abs(p2*(p2-a[2])*(p2-a[3])*(p2-a[4])));
-
- }
- }
-
- vol[i]=(area[0]+area[5])*0.5+area[1]+area[2]+area[3]+area[4];
- if(suf[i]<min)
- {
- min=suf[i];
- flag=i;
- for(int t=0;t<122;t++)
- {
- for(int q=0;q<60;q++)
- {
- fin[t][q]=ran[t][q];
- fin1[t][q]=ran1[t][q];
- }
- }
- }
- //cout<<suf[i]<<" ";
- i--;
- }
- cout<<flag<<" "<<min<<" "<<vol[flag]<<" "<<suf[flag]<<endl;
-
- for(int t=0;t<60;t++)
- {
- for(int i=0;i<122;i++)
- {
- out<<0.5*((double)i+((double)(122-i)*head[t]/(double)122))<<" "<<fin[i][t]<<" "<<(double)t<<endl;
- out<<0.5*((double)i+((double)(122-i)*head[t]/(double)122))<<" "<<-fin[i][t]<<" "<<(double)t<<endl;
- out<<0.5*((double)244-(double)i-((double)(122-i)*head2[t]/(double)122))<<" "<<fin1[i][t]<<" "<<(double)t<<endl;
- out<<0.5*((double)244-(double)i-((double)(122-i)*head2[t]/(double)122))<<" "<<-fin1[i][t]<<" "<<(double)t<<endl;
- }
- cout<<endl;
- }
- out.close();
- }
matlab仿真代码如下:
- 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('Parent',figure1);hold(axes1,'on');% 创建 meshmesh(xdata1,ydata1,zdata1,cdata1,'Parent',axes1);% 创建 plot3plot3(X1,Y1,Z1,'MarkerSize',15);% 创建 titletitle('船体模型');view(axes1,[-37.5 30]);grid(axes1,'on');axis(axes1,'tight');hold(axes1,'off');% 设置其余坐标区属性set(axes1,'DataAspectRatio',[1 1 1]);
- 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,'linear');%对Z进行拟合mesh(X,Y,Z) %interpolatedaxis equal;axis tight; hold on%设置坐标轴plot3(x,y,z,'MarkerSize',15) %nonuniform%进行绘制三维图形title('船体模型');% plot3(x,y,z)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。