Output argument "v" (and maybe others) not assigned during call to "Deflection_vmndangi".
4 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
dpb
on 20 Sep 2019
Edited: dpb
on 20 Sep 2019
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 Comments
dpb
on 20 Sep 2019
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!