Output argument "v" (and maybe others) not assigned during call to "Deflection_vmndangi".

조회 수: 3 (최근 30일)
function [v] = Deflection_vmndangi(x)
% Author : Vainquer Ndangi
% Date : September 22, 2019
% Purpose : Calculationg Deflection
% Inputs:
% x: position
% Outputs:
% v : Deflection
if (x > 0 & x <= 120)
v = (1./(3.19 .* 10.^9)) .* (800 .* x.^3 - 13.68 .* 10.^6 .* x - 2.5 .* x.^4);
end
if (x > 120 & x <= 240)
v = (1./(3.19 .* 10.^9)) .* (800 .* x.^3 - 13.68 .* 10.^6 .* x - 2.5 .* x.^4 + (25 .* (x - 120).^4));
end
if (x > 240 & x <= 360)
v = (1./(3.19 .* 10.^9)) .* (800 .* x.^3 - 13.68 .* 10.^6 .* x - 2.5 .* x.^4 + (25 .* (x - 120).^4) + (600 .* (x - 240).^3));
end
end
every time i run this function with a single value it works. However, when i try to run it with a vector it only works if every element of the vector are under the same condition. if one element is not it will return "Output argument "v" (and maybe others) not assigned during call to "Deflection_vmndangi".". what can i do?

채택된 답변

dpb
dpb 2019년 9월 20일
편집: dpb 2019년 9월 20일
None of the conditions will be met if you pass a vector as you have written the IF tests. IF is true iff (if and only if) all elements of the logical vector that results from the comparison are true--if you pass a vector of arguments that don't all satisfy the same condition, at least one member of that test will be false for every case and so lacking an "else" clause or assignment for that case, the output v is never assigned.
You need something like
function [v] = Deflection_vmndangi(x)
% Author : Vainquer Ndangi
% Date : September 22, 2019
% Purpose : Calculationg Deflection
% Inputs:
% x: position
% Outputs:
% v : Deflection
v=zeros(size(x));
ix=(x>0 & x<=120);
v(ix) = (800*x(ix).^3 - 13.68E6*x(ix) - 2.5*x(ix).^4)/3.19E9 ;
ix=(x > 120 & x <= 240);
v(ix) = (800*x(ix).^3 - 13.68E6*x(ix) - 2.5*x(ix).^4 + 25*(x(ix)-120).^4)/3.19E9;
ix=(x>240 & x<=360);
v(ix) = (800*x(ix).^3 - 13.68E6*x(ix) - 2.5*x(ix).^4 + 25*(x(ix)-120).^4 + 600*(x(ix)-240).^3)/3.19E9;
end
so that each element is computed by the given correlation.
See the doc for if and for "logical indexing" for more details.
NB: the "dot" operators are not needed for multiplication by a constant and it's much more efficient to write the coefficient magnitude as a literal constant rather than computing the powers of 10.
  댓글 수: 4
dpb
dpb 2019년 9월 20일
Slightly more efficient, maybe...
function [v] = Deflection_vmndangi(x)
% Author : Vainquer Ndangi
% Date : September 22, 2019
% Purpose : Calculationg Deflection
% Inputs:
% x: position
% Outputs:
% v : Deflection
v = (800*x.^3 - 13.68E6*x - 2.5*x.^4)/3.19E9 ;
ix=(x > 120 & x <= 240);
if any(ix)
v(ix) = V(ix) + 25*(x(ix)-120).^4/3.19E9;
end
ix=(x>240 & x<=360);
if any(ix)
v(ix) = v(ix) + 600*(x(ix)-240).^3/3.19E9;
end
end
Still no testing of bounds on inputs but only does the sections needs to.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by