# Convert Matlab Math Trig into C#

• ### Question

• How to convert MATLAB code below into c# code below. I need some assistance

theta=0:pi/180:2*pi;

costheta=cos(theta);

pianoboyCoder

Wednesday, February 6, 2019 8:14 AM

### All replies

• Hi pianoboyCoder,

It will be beneficial to resolve the issue if you add comment to describe the following code.

`theta=0:pi/180:2*pi;`

In addition, you could refer to Math method in .net framework, which provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions. Such as.

```// PI
var pi = Math.PI;
//Cos
var costheta = Math.Cos(pi);```

Best regards,

Zhanglong

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Wednesday, February 13, 2019 2:39 AM
Thursday, February 7, 2019 1:53 AM
• I admit … I know NOTHING about MATLAB, but predicated on the information at

https://www.tutorialspoint.com/matlab/matlab_colon_notation.htm

I would think the C analog would be something like …

```            const double increment = Math.PI/180.0;
const double upperLimit = 2.0 * Math.PI;
double cosTheta;

for (double theta = 0;
theta <= upperLimit;
theta += increment)
cosTheta = Math.Cos(theta);
```

Friday, February 8, 2019 3:54 AM
• Almost.  The Matlab code will create costheta as an array with 360 elements.

Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

Monday, February 11, 2019 7:04 AM