Info

此问题已关闭。 请重新打开它进行编辑或回答。

Trying to convert this C++ Code to work in MatLab

1 次查看(过去 30 天)
Hello, I am currently trying to get this c++ code to work in Matlab so I can understand the differences between the two programming systems. Can someone please show me what the code for the following C++ code would be in Matlab. I upladed a picture as well as simply copied in the text so you can easily copy it as well as uploaded picture from problem. Thank you
Computer program: (C language)
#include<stdio.h>
#include<conio.h>
double lx=10.0;
int n=20;
main()
{
int i;
double h,Ta,dx=0;
double To[100]={0},T[100]={0},error=100;
dx=lx/n;
h=0.02;
Ta=20;
while(error>1.0e-10)
{
for(i=1;i<=n;i++)
{
To[i]=T[i];
}
T[0]=40.0; To[0]=40.0;
T[n]=200.0; To[n]=200.0;
for(i=1;i<=n-1;i++)
{
T[i]=(To[i+1]+To[i-1]+h*dx*dx*Ta)/(2+h*dx*dx);
}
float e=0,eold=0;
for(i=1;i<=n-1;i++)
{
e=T[i]-To[i];
if(e>eold)
{
eold=e;
error=e;
}
}
}
printf("\n\nFINAL RESULT\n\n");
for(i=0;i<=n;i++)
{
printf("%f\t",T[i]);
}
}
​​​​​​​Temperature at the internal nodes:
The temperature convergence is set to 10-10 for all the cases.
  1 个评论
Steven Lord
Steven Lord 2020-7-21
This sounds like a homework assignment. If it is, please show us the MATLAB code you've written so far and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.

回答(0 个)

此问题已关闭。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by