MATLAB Answers

I am working on a certain problem but the end result is a single value repeated for n elements.

2 views (last 30 days)
Kundan Sanjeeth Dandangi
Kundan Sanjeeth Dandangi on 1 Apr 2020
Edited: dpb on 1 Apr 2020
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.

  7 Comments

Show 4 older comments
Kundan Sanjeeth Dandangi
Kundan Sanjeeth Dandangi on 1 Apr 2020
The m value is supposed to take each value from w and then the for loop is supposed to iterate for 6 values (or 4 as i have assigned 4 values to w). How do i script for the latter mentioned.
dpb
dpb on 1 Apr 2020
In that case, you have to move TauLAB inside the loop and use w(i). There's no point in having another variable, m, here.
...
N=4; % don't bury "magic numbers" inside code; make variables so can change
Sy = zeros(N,1);
dg = linspace(10,102,N);
for i = 1:N
TauHP = Khp/((dg(i))^nhp);
TauLAB = (a2*Mu*b)/w(i);
TauB = Tauf + Tauss + TauLAB + TauHP;
%Yield strength
Sy(i) = M*hypot(TauA,TauB);
end
With those corrections, Sy does change albeit by very small amount...
>> Sy
Sy =
1.0e+08 *
3.7798
3.7798
3.7798
3.7798
>> diff(Sy)
ans =
1.0e-04 *
-0.7296
-0.0679
0.0018
>>
There's only a change in 1:10^12 relative magnitude. This may be expected result or indicative of a problem in the coding of the equations or a scaling or units mismatch. Can't comment on that problem (if it is a problem, even).

Sign in to comment.

Accepted Answer

dpb
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).

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags

Products


Translated by