Why doesn't my code recognize my function outputs

1 visualización (últimos 30 días)
Kathryn Pacheco
Kathryn Pacheco el 14 de Feb. de 2020
Respondida: Star Strider el 14 de Feb. de 2020
Whenever I try to run my code I get an error saying that survivalProb does not exist but its an output
Im trying to create a loop that codes serveral different lines but I'm only gettig one line
clear
close all
clc
% Initialize data for component
d=.005; % diameter in m
A=(pi/4)*d^2; % bolt cross-sectional area
IdealS=750000; % ideal strength in Pa
% Set load schedule
F= [10:.1:20]; % forces
%limits of each stresses
S=F./A;
% stresses
% Sample strength variability
var = [.01 .12 .04 .08]; % percent variability in strength for each batch
nSamp = [100 150 125 95]; % number of samples in each batch
for J=1:4
testStats(IdealS, var(J), nSamp(J), S);
plot(F,survivalProb,'LineWidth',4)
end
grid on
xlabel('Force Applied')
ylabel('Survival Probability')
function [survivalProb, avgSurvival] = testStats(IdealS,var,nSamp,S)
S2=S-S*var;
S1=S+S*var;
S=(S1-S2)*rand+ S2;
survivalProb= S<=IdealS;
survivalProb=double(survivalProb)
avgSurvival=sum(survivalProb,'all')/nSamp
end

Respuestas (1)

Star Strider
Star Strider el 14 de Feb. de 2020
Yopu code does not specifically ask for that variable.
Try this instead:
for J=1:4
[survivalProb, avgSurvival] = testStats(IdealS, var(J), nSamp(J), S);
plot(F,survivalProb,'LineWidth',4)
end
That returns both outputs.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by