鱼C论坛

 找回密码
 立即注册
查看: 2664|回复: 8

写的一个代码求求大神帮忙看一下哪里有问题把,救救孩子吧。

[复制链接]
发表于 2020-3-23 10:26:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import math
h=0.1
r=0.05
m=r/(h*h)
x=0
t=0
u=[]
print('x数值'.ljust(8),'t数值'.ljust(8),'u数值'.ljust(8))
for a in range(101):
    u.append([])
    for b in range(201):
        u[a].append(0)
while (x==0 and x<101):
    u[x][0]=math.exp(x*h)
    x=x+1
while (t==0 and t<201):
    u[0][t]=math.exp(t*r)
    u[100][t]=math.exp(t*r+1)
    t=t+1
while (x==1 and x<101):
    x=x+1
    while (t==1 and t<201):
        u[x][t]=(1-2*m)*u+m*(u[x+1][t]+u[x-1][t])
        t=t+1
while (x==0 and x<101):
    x=x+1
    while (t==0 and t<201):
        t=t+1
        print("%.8f" % x,"%.8f" % t ,"%.8f" % u[x][t]  )
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-23 10:34:24 | 显示全部楼层
问题呢??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 10:37:08 | 显示全部楼层
x数值      t数值      u数值     
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-28d82d7126c7> in <module>
     21     x=x+1
     22     while (t==1 and t<201):
---> 23         u[x][t]=(1-2*m)*u+m*(u[x+1][t]+u[x-1][t])
     24         t=t+1
     25 while (x==0 and x<101):

TypeError: can't multiply sequence by non-int of type 'float'

这是他的反馈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 10:38:26 | 显示全部楼层

x数值      t数值      u数值     
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-28d82d7126c7> in <module>
     21     x=x+1
     22     while (t==1 and t<201):
---> 23         u[x][t]=(1-2*m)*u+m*(u[x+1][t]+u[x-1][t])
     24         t=t+1
     25 while (x==0 and x<101):

TypeError: can't multiply sequence by non-int of type 'float'

这是它的反馈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-23 11:04:53 From FishC Mobile | 显示全部楼层
头铁学习 发表于 2020-3-23 10:38
x数值      t数值      u数值     
---------------------------------------------------------------- ...

你想实现啥呢
对于你的代码,无法理解,x==0同时x还要<???
那不就是x=0吗?那为啥还要用循环呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 11:22:59 | 显示全部楼层
wp231957 发表于 2020-3-23 11:04
你想实现啥呢
对于你的代码,无法理解,x==0同时x还要

我希望用Python实现以下功能 但是我刚接触编程 感觉很困难
#include<stdio.h>
#include<math.h>
void main()
{
        FILE *fp;
        fp=fopen("xs.txt","w");
        int j,k;
        float r,u[51][201];
        float h=0.1,t=0.005;
        r=t/pow(h,2);
        for(k=0;k<51;k++)
        {
                u[k][0]=exp(k*h);
        }
        for(j=0;j<201;j++)
        {
                u[0][j]=exp(j*t);
                u[50][j]=exp(1+j*t);
        }
        for(j=1;j<201;j++)
        {
                for(k=1;k<50;k++)
                {
                        u[k][j]=(1-2*r)*u[k][j-1]+r*(u[k+1][j-1]+u[k-1][j-1]);
                }
        }
        for(j=0;j<201;j++)
        {
                for(k=0;k<51;k++)
                {
                        fprintf(fp,"%f %f %f\n", j*t, k*h,u[k][j]);
                }
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-23 11:25:01 From FishC Mobile | 显示全部楼层
头铁学习 发表于 2020-3-23 11:22
我希望用Python实现以下功能 但是我刚接触编程 感觉很困难
#include
#include

啥功能你倒是说啊,你拿一坨代码干嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 11:34:50 | 显示全部楼层
wp231957 发表于 2020-3-23 11:25
啥功能你倒是说啊,你拿一坨代码干嘛

嗯就是当(x<=10)时u(x,o)=e^x
(t<=10)时u(0,t)=e^t,u(10,t)=e^(1+t)
其余项u[x][t]=(1-2*m)*u+m*(u[x+1][t]+u[x-1][t])
然后打印出使用u的值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 11:35:39 | 显示全部楼层

大神 这是我们微分方程课程的一个作业 但是之前没有接触过编程所以实在有点头疼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 23:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表