2 views (last 30 days)

for i = [10:50]

TauHP = Khp/((dg(i))^nhp);

TauB = Tauf + Tauss + TauLAB + TauHP;

%Yeild strength

disp(TauB);

Sy(i) = M*(((TauA^2) + (TauB^2))^0.5);

end

I'm assuming there is a mistake in this for loop as rest of the code is all about constants. The Sy(i) should change linearly with dg(i), but it isn't the case.

dpb
on 1 Apr 2020

Incorporating comments from the stream above in result...using your code and loop would look something like

%%Task 1

%Assigning constants

M = 2.9;

a1 = 0.2;

a2 = 2.0;

a = 2.867;

Khp = 6.16;

nhp = 0.5;

Kc = 0.19;

Kmn = 0.048;

Mu = 83;

Tauf = 0.0083;

w = [4.1,6.66667,7.23333,7.2].'; %width values in micrometers

Strain_rate = 0.01; %Constant strain rate

dfi = 1*10^13; %Dislocation density for ferritic iron

b = a*sqrt(3)/2;

TauA = a1*Mu*b*sqrt(dfi);

Tauss = 0.006*Kc;

%TauGB = TauHP + TauLAB

N=4; % set as variable, don't bury magic constants in code

Sy = zeros(N,1);

dg = linspace(10,102,N);

for i = 1:N

TauLAB = (a2*Mu*b)/m; %m = mean slip length

TauHP = Khp/((dg(i))^nhp);

TauB = Tauf + Tauss + TauLAB + TauHP;

%Yield strength

% disp(TauB);

Sy(i) = M*hypot(TauA,TauB); % use builtin hypot function instead explicit code...shorter

end

disp(Sy);

plot (dg,Sy);

Alternatively, "the MATLAB way" once you've defined all the constants is to vectorize such calculations--

TauHP = Khp./dg.^nhp;

TauLAB=a2*Mu*b./w;

TauB = Tauf + Tauss + TauLAB + TauHP;

vSy=M*hypot(TauA,TauB); % NB: the variable name change so can...

>> all(vSy==Sy) % prove get same answer either way...

ans =

logical

1

>>

For real case, use the variable Sy for the vectorized version as well, of course. Just kept so could show the equivalence.

The above trades memory in having the intermediate variables all as arrays for speed and less code in eliminating the loop and the indexing of variables inside the loop for the temporaries computed in the loop but not saved.

An alternative since is only the dg and w that are variables would be to write

fnTauHP = @(dg) Khp./dg.^nhp;

fnTauLAB= @(w) a2*Mu*b./w;

as anonymous functions then call them with the w and dg arrays in the overall function.

Either way, the result is the same as shown in the comment above that while there is a change in Sy, it is extremely small in relation to the magnitude of Sy; can't judge on that part of the problem (again, if it is a problem).

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819388

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819388

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819395

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819395

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819405

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819405

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819410

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819410

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819415

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819415

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819459

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819459

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819461

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/514622-i-am-working-on-a-certain-problem-but-the-end-result-is-a-single-value-repeated-for-n-elements#comment_819461

Sign in to comment.