简体   繁体   English

Matlab三角函数

[英]Matlab trigonometric functions

I am fairly new to Matlab. 我是Matlab的新手。 I wrote a code in C++ to calculate the length of the base of triangle in respect to alpha angle, the code works as expected. 我用C ++编写了一个代码来计算三角形底边相对于alpha角的长度,代码按预期工作。 Now I would like to complete the same task in Matlab and plot results. 现在我想在Matlab中完成相同的任务并绘制结果。

C++ code C ++代码

        #include <iostream>
        using namespace std;
        #include <tgmath.h> 

        int a = 135; 
        int b = 125;

        double base;
        double alphaB;
        double alphaC;
        double alphaA;
        float difference;

        #define PI 3.14159265358979323846

        int main() {
            for(int i = 1; i < 90; i++){

               alphaA = i * PI/180;
               alphaB = asin((b*sin(alphaA))/a);
               alphaC = 180*PI/180 - alphaA - alphaB;
               base = (a*sin(alphaC))/(sin(alphaA));

               cout << base << endl;
            }
            return 0;
        }

Matlab script Matlab脚本

y = 0:1:90;
z = my(y);
plot(z,y)

function base = my(x)

a = 135; %Side A lenght
b = 125; %Side B lenght

 f = x * pi/180;
 alphaB = asin((b*sin(f))/a);
 alphaC = 180*pi/180 - f - alphaB;
 base   = (a*sin(alphaC))/(sin(f));
 disp(base);
end

when I run the Matlab script the output is just one number and the plot is empty, so I wrote another debugging script stripping "(a*sin (alpha)) /(sin (f))" equation into pieces 当我运行Matlab脚本时,输出只是一个数字而且图是空的,所以我写了另一个调试脚本,将“(a * sin(alpha))/(sin(f))”方程式分解成碎片

y = 1:1:5;
z = my(y);
plot(z,y)

function base = my(x)

a = 135; %Side A lenght
b = 125; %Side B lenght

 f = x * pi/180;
 alphaB = asin((b*sin(f))/a);
 alphaC = 180*pi/180 - f - alphaB;
 alphaC2 = sin(alphaC);
 sf = sin(f);
 aa = a*alphaC2;
 test = aa/sf;  % division problem
 base   = aa;
 disp(test);
 disp(aa);
 disp(sf);

end

It turns out the problem is when it comes to division, when displaying "aa,sf" I got all of the output, 90 numbers. 事实证明问题在于划分,当显示“aa,sf”时,我得到了所有的输出,90个数字。 When the script hits "test = aa/SF" I have single output(one number). 当脚本命中“test = aa / SF”时,我有单输出(一个数字)。 How to overcome this problem? 如何克服这个问题?

You would want to use ./ instead of / 您可能希望使用./而不是/

/ is a matrix division ./ is a division of each element. /是矩阵除法./是每个元素的一个除法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM