Why doesn't my code recognize my function outputs

조회 수: 7 (최근 30일)
Kathryn Pacheco
Kathryn Pacheco 2020년 2월 14일
답변: Star Strider 2020년 2월 14일
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

답변 (1개)

Star Strider
Star Strider 2020년 2월 14일
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.

카테고리

Help CenterFile Exchange에서 Stress and Strain에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by