Value of index return number

9 visualizaciones (últimos 30 días)
John Slugg
John Slugg el 11 de Abr. de 2020
Respondida: Walter Roberson el 12 de Abr. de 2020
I am currently working on an assignment for which I need to write loops for indexed values.
The problem involes a differential equation for which I need results for itteration at specific, non-uniform increments.
Nominal stress (stress_nom) is a parameter driven by these intervals, but I cannot get it to return a values for stress_nom.
For context I have attached a graph indicating nominal stress in the problem.
%% Index for loop
syms idx
idx_list = [0:1:30]
for n = 1:numel(idx)
idx = idx_list(n);
stress_nom = piecewise((0<idx)&&(idx<=1),46,(1<idx)&&(idx<=3),26,(3<idx)&&(idx<=6),16,(6<idx)&&(idx<=23),12,(23<idx)&&(idx<=26),24,(26<idx)&&(idx<=27),26,(27<idx)&&(idx<=29),16);
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Abr. de 2020
You are overwriting all of the variable stress_nom on each iteration of your for n loop.
You initialize syms idx but then inside your loop you have idx = idx_list(n). idx_list is double precision, so (0<idx)&&(idx<=1) is double precision, but you can only piecewise() symbolic expressions.
In the special case where the inputs are numeric and not infinite, you can use a trick:
idx = 0:1:30;
stress_nom = ((0<idx)&(idx<=1)) .* 46 + ...
((1<idx)&(idx<=3)) .* 26 + ...
((3<idx)&(idx<=6)) .* 16 + ...
((6<idx)&(idx<=23)) .* 12 + ...
((23<idx)&(idx<=26)) .* 24 + ...
((26<idx)&(idx<=27)) .* 26 + ...
((27<idx)&(idx<=29)) .* 16;
but you might as well just use
[nan 46 26 26 16 16 16 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 24 24 24 26 16 16 nan]
unless your idx will take on other values as well.
Notice that you have not defined a value for 0 or 30.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by