下列代码出现nan int错误,请解决:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }

时间: 2024-03-23 22:37:36 浏览: 15
在这段代码中,出现nan和inf通常是由于除以0或开根号时计算负数引起的。你可以在运行代码之前添加一些检查,比如检查cot_weight是否为0,以避免除以0的情况。另外,你还可以在计算cot_alpha和cot_beta之前,添加一个检查,确保两个点之间的距离不为0,以避免计算负数的情况。具体来说,你可以修改代码如下: ``` float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double dist1 = (curr_pi - prev_p).norm(); double dist2 = (curr_pj - prev_p).norm(); double dist3 = (curr_pi - next_p).norm(); double dist4 = (curr_pj - next_p).norm(); if (dist1 > 0 && dist2 > 0 && dist3 > 0 && dist4 > 0) { double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; } } cog += cot_weight * (mesh.point(*vv_it) - mesh.point(*v_it)); valence += cot_weight; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } ```

相关推荐

请将下列代码改为可在vs中运行的版本:double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } //cot平滑 float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); valence += cot_weight; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } void smoothCot(float threshold) { float err; do { err = smoothCot(); cout << "err:" << err << endl; } while (err >= threshold); }

在下列代码中如何表示与v_it及vv_it相邻的两个点,请写出具体代码:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }

为什么在下列代码中如何表示与*v_it与prev_vh表示的是同一个点:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }

请逐行解释下列代码:double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } //cot平滑 float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); valence += cot_weight; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } void smoothCot(float threshold) { float err; do { err = smoothCot(); cout << "err:" << err << endl; } while (err >= threshold); }

为什么在下列代码中如何表示与curr_pi与prev_p表示的是同一个点:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }

在下列代码中如何表示与v_it及vv_it相邻的两个点float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }

import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import matplotlib.pyplot as plt # 定义RBF神经网络的类 class RBFNetwork(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RBFNetwork, self).__init__() # 初始化输入层,隐含层,输出层的节点数 self.input_size = input_size self.hidden_size = hidden_size self.output_size = output_size # 初始化权重矩阵和偏置向量 self.W1 = nn.Parameter(torch.randn(input_size, hidden_size)) # 输入层到隐含层的权重矩阵 self.b1 = nn.Parameter(torch.randn(hidden_size)) # 隐含层的偏置向量 self.W2 = nn.Parameter(torch.randn(hidden_size, output_size)) # 隐含层到输出层的权重矩阵 self.b2 = nn.Parameter(torch.randn(output_size)) # 输出层的偏置向量 def forward(self,x): # 前向传播过程 x = torch.from_numpy(x).float() # 将输入向量转换为张量 x = x.view(-1, self.input_size) # 调整输入向量的形状,使其与权重矩阵相匹配 h = torch.exp(-torch.cdist(x, self.W1.t()) + self.b1) # 计算隐含层的输出值,使用高斯径向基函数作为激活函数 y = F.linear(h, self.W2.t(), self.b2) # 计算输出层的输出值,使用线性函数作为激活函数 return y #定义pid控制器 class Pid(): def __init__(self, exp_val, kp, ki, kd): self.KP = kp self.KI = ki self.KD = kd self.exp_val = exp_val self.now_val = 0 self.sum_err = 0 self.now_err = 0 self.last_err = 0 def cmd_pid(self): self.last_err = self.now_err self.now_err = self.exp_val - self.now_val self.sum_err += self.now_err self.now_val = self.KP * (self.exp_val - self.now_val) \ + self.KI * self.sum_err + self.KD * (self.now_err - self.last_err) return self.now_val def err_pid(self): self.last_err = self.now_err self.now_err = self.exp_val - self.now_val self.sum_err += self.now_err self.p_err = self.exp_val - self.now_val self.i_err = self.sum_err self.d_err = self.now_err - self.last_err self.now_val = self.KP * (self.exp_val - self.now_val) \ + self.KI * self.sum_err + self.KD * (self.now_err - self.last_err) return self.p_err, self.i_err, self.d_err rbf_net = RBFNetwork(3,10,4) pid_val = [] #对pid进行初始化,目标值是1000 ,p=0.1 ,i=0.15, d=0.1 A_Pid = Pid(1000, 0.1, 0.1, 0.1) # 然后循环100次把数存进数组中去 for i in range(0, 100): input_vector = np.array(A_Pid.err_pid()) output_vector = rbf_net(input_vector) output_vector = output_vector.reshape(4,1) A_Pid = Pid(1000, output_vector[0], output_vector[1], output_vector[2]) pid_val.append(A_Pid.cmd_pid())

最新推荐

recommend-type

Android 出现:java.lang.NoClassDefFoundError...错误解决办法

主要介绍了Android 出现:Android出现:java.lang.NoClassDefFoundError: android/os/PersistableBundle错误解决办法的相关资料,需要的朋友可以参考下
recommend-type

解决vue net :ERR_CONNECTION_REFUSED报错问题

主要介绍了解决vue net :ERR_CONNECTION_REFUSED报错问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

java.net.ConnectException: Connection refused问题解决办法

主要介绍了java.net.ConnectException: Connection refused问题解决办法的相关资料,需要的朋友可以参考下
recommend-type

MySQL复制出错 Last_SQL_Errno:1146的解决方法

主要介绍了MySQL复制出错 Last_SQL_Errno:1146的解决方法,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依