MATLAB Answers

Why is MATLAB producing the wrong output with this function?

3 views (last 30 days)
Gavin Thompson
Gavin Thompson on 16 Sep 2021
Commented: Gavin Thompson on 16 Sep 2021
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.

Accepted Answer

Chunru
Chunru on 16 Sep 2021
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by