MATLAB Answers

Why doesn't my code recognize my function outputs

3 views (last 30 days)
Kathryn Pacheco
Kathryn Pacheco on 14 Feb 2020
Answered: Star Strider on 14 Feb 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

  0 Comments

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Feb 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.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by