Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
random1072
random1072 on 21 Jul 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
Steven Lord
Steven Lord on 21 Jul 2020
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.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!