Conversion of C++ code to matlab code
35 次查看(过去 30 天)
显示 更早的评论
I've a C++ code to be converted to MATLAB script. Following is the code. Please assist in the conversion process. I've tried my part in conversion which I've attached
#include <iostream>
#include <math>
#include <complex>
#include <bits/stdc++.h>
#include <bits>
using namespace std;
void main()
{
const k=10;
const Rmn=12;
float phi=0;
float theta=90;
float phimn=14;
float thetamn=16;
int q=34;
const dx=23;
const dy=25;
int m,n;
cout<<"Enter the value of M"
cin>>m
cout<<"Enter the value of N"
cin>>n
int FinalAF=0;
for(int i=-m+1;i<=m;i++)
{
for(int j=-n+1;j<=n;j++)
{
// e1=sin(-1*k*Rmn)+jcos(-1*k*Rmn)
// e2=cos(phimn)+jsin(phimn)
complex<double> compl1(0,-1*k*Rmn);
complex<double> compl2(0,phimn);
Inter=exp(compl1)*exp(compl2)/Rmn;
Imn=Inter*cos(thetamn)^q;
float xmn=(m-0.5)*dx;
float ymn=(n-0.5)*dy;
string rmn = to_string(xmn)+"x"+to_string(ymn)+"y"
string rc = to_string(sin(theta)*cos(phi))+"x"+to_string(sin(theta)*cos(phi))+"y"+to_string(sin(theta)*cos(phi))+"z"
complex<double> compl3(0, rmn*rc*k);
AF=Imn*exp(compl3);
FinalAF=FinalAF+AF;
}
}
int Etheta = FinalAF*cos(theta);
}
% The code that I tried converting from C++ to MATLAB
%k=10;
%rmn=12;
%phi=0;
%theta=90;
%thetamn=16;
%phimn=14;
%q=34;
%dx=23;
f=input('Enter the frequency in GHz');
rmn=input('Enter the distance form feed phase center to “mnth” element');
phimn=input('Enter the phase provided by the mnth element of microstrip reflectarray array');
q=input('Enter q for evaluating the cosine power radiation pattern');
epsilonr=2.2;
c = 3*1e8;
lambda0 = c/(f*1e9);
lambdag = lambda0/sqrt(epsilonr);
dx = lambdag/2;
k = 2*pi/lambda0;
M=input('Enter the number of rows');
N=input('Enter the number of columns');
string r = string(sin(theta)*cos(phi))+"x"+ string(sin(theta)*cos(phi))+"y"+ string(sin(theta)*cos(phi))+"z";
for m= -M+1:M
for n= -N+1:N
g=phimn-k*rmn;
Imn=(exp(1i*g)*(cos(phimn)^q))/rmn;
end
end
for m = -(M+1):M
for n = -(N+1):N
AF = Imn*exp(1i*(rmn.*r)*k);
% .* = dot product of rmn vector and r vector
end
end
% let radiation pattern be E
for m = -(M+1):M
for n = -(N+1):N
E = AF*cos(phimn);
end
end
6 个评论
Walter Roberson
2020-1-14
You are trying to multiply STRINGS. You are not creating two three-D vectors and multiplying them: you are calculating the numeric portions of the vectors and converting them to text and adding in the characters 'x' and 'y' and 'z' in appropriate places, and then you are trying to multiply the text !
Guillaume
2020-1-14
编辑:Guillaume
2020-1-14
edit: I need my eyes tested, there's indeed a multiplication between strings in the C++ code which is completely meaningless. The C++ code couldn't have worked.
If I were given the task to convert code from one language to matlab, I would do it like this:
- Understand what the code does
- Write down the algorithm/equation implemented by said code
- throw away that code and never look at it again
- implement the algorithm/equation from step 2 in matlab
I would certainly not attempt to convert the code line by line. Attempting to write matlab code as if it was C++ is going to lead to very poor code.
As it appear you already have the equations, I would start from there and forget about the C++ code.
The equations can be implemented very easily in matlab without any loop. This will make it much easier to read as well. However, it's not clear to me why has a vector arrow above it nor what , , and are.
回答(2 个)
Bhaskar R
2020-1-14
You have not used loop iteration varibales in C++ code?, anyway the plane conversion of your code is
% using namespace std;
% void main()
% {
% const k=10;
k = 10;
% const Rmn=12;
Rmn=12;
% float phi=0;
phi=0;
% float theta=90;
theta=90;
% float phimn=14;
phimn=14;
% float thetamn=16;
thetamn=16;
% int q=34;
q=34;
% const dx=23;
dx=23;
% const dy=25;
dy=25;
% int m,n;
% cout<<"Enter the value of M"
% cin>>m
m = input('Enter the value of M : ');
% cout<<"Enter the value of N"
% cin>>n
n = input('Enter the value of N : ');
% int FinalAF=0;
FinalAF=0;
% for(int i=-m+1;i<=m;i++)
% {
for ii = -m+1:m
% for(int j=-n+1;j<=n;j++)
% {
for jj = -n+1:n
% // e1=sin(-1*k*Rmn)+jcos(-1*k*Rmn)
% // e2=cos(phimn)+jsin(phimn)
% complex<double> compl1(0,-1*k*Rmn);
compl1 = 0+(-1*k*Rmn)*1i;
% complex<double> compl2(0,phimn);
compl2 = 0+(phimn)*1i;
%Inter=exp(compl1)*exp(compl2)/Rmn;
Inter = exp(compl1)*exp(compl2)/Rmn;
% Imn=Inter*cos(thetamn)^q;
Imn=Inter*cos(thetamn)^q;
% float xmn=(m-0.5)*dx;
xmn=(m-0.5)*dx;
% float ymn=(n-0.5)*dy;
ymn=(n-0.5)*dy;
% string rmn = to_string(xmn)+"x"+to_string(ymn)+"y"
rmn = [num2str(xmn), 'x', num2str(ymn), 'y'];
% string rc = to_string(sin(theta)*cos(phi))+"x"+to_string(sin(theta)*cos(phi))+"y"+to_string(sin(theta)*cos(phi))+"z"
rc = [num2str(sin(theta)*cos(phi)), 'x',...
num2str(sin(theta)*cos(phi)), 'y', num2str(sin(theta)*cos(phi)), 'z'];
% complex<double> compl3(0, rmn*rc*k);
compl3 = 0+ (rmn*rc*k)*1i;
%AF=Imn*exp(compl3);
AF=Imn*exp(compl3);
% FinalAF=FinalAF+AF;
FinalAF=FinalAF+AF;
end
end
% int Etheta = FinalAF*cos(theta);
Etheta = FinalAF*cos(theta);
3 个评论
Walter Roberson
2020-1-14
Which variable is to be on the x axes? Which variable is to be on the y axes? Which variable is to be on the z axes?
rania craise
2021-1-28
please is there any way to convert the program from a C++ language to matlab ?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!