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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by