Why is MATLAB producing the wrong output with this function?

1 回表示 (過去 30 日間)
Gavin Thompson
Gavin Thompson 2021 年 9 月 16 日
コメント済み: Gavin Thompson 2021 年 9 月 16 日
So using these piecewise functions, I am trying to find v(x) which is beamDef in my function, given a user inputted value of x which is beamLoc.
function [beamDef] = FindDeflection(beamLoc)
A = (1/3.19e9);
x1 = (beamLoc>0)&(beamLoc<=120);
x2 = ((beamLoc>120)&(beamLoc<=240));
x3 = ((beamLoc>240)&(beamLoc<=360));
beamDef = A*((800*(x1.^3))-(13.68.*(10^6).*x1)-(2.5*(x1.^4)));
beamDef = A*((800*(x2.^3))-(13.68.*(10^6).*x2)-(2.5*((x2.^4))+(2.5.*(x2-120).^4)));
beamDef = A*((800*(x3.^3))-(13.68.*(10^6).*x3)-(2.5*((x3.^4))+(2.5.*(x3-120).^4))+(600.*((x3-240).^3)));
end
While trying to test when beamLoc = 115 on my main script, it gives me a beamDef = -2.763 when it should actually be -0.249. I've tried rewriting the equations given in many different ways but I still can't get MATLAB to produce the correct output.

採用された回答

Chunru
Chunru 2021 年 9 月 16 日
beamLoc = [10 200 300]
beamLoc = 1×3
10 200 300
b = FindDeflection((beamLoc))
b = 1×3
-0.0426 -0.1374 -1.6454
function [beamDef] = FindDeflection(beamLoc)
A = (1/3.19e9);
beamDef = zeros(size(beamLoc)); % initialize the result
i1 = (beamLoc>0)&(beamLoc<=120);
x1 = beamLoc(i1);
i2 = ((beamLoc>120)&(beamLoc<=240));
x2 = beamLoc(i2);
i3 = ((beamLoc>240)&(beamLoc<=360));
x3 = beamLoc(i3);
beamDef(i1) = A*((800*(x1.^3))-(13.68.*(10^6).*x1)-(2.5*(x1.^4)));
beamDef(i2) = A*((800*(x2.^3))-(13.68.*(10^6).*x2)-(2.5*((x2.^4))+(2.5.*(x2-120).^4)));
beamDef(i3) = A*((800*(x3.^3))-(13.68.*(10^6).*x3)-(2.5*((x3.^4))+(2.5.*(x3-120).^4))+(600.*((x3-240).^3)));
end
  1 件のコメント
Gavin Thompson
Gavin Thompson 2021 年 9 月 16 日
Thank you so much! It's finally working.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by