Help with For loop during least square fit
4 views (last 30 days)
Show older comments
Hello,
I want to linearize my stress-number of cycles plot.
I have my StressAmp and FatigueLife-vectors. The thing I want help with my for-loop.
I want to calculate BTak which is
The sum of when Xi and Yi goes from 1 to the maximum of X and Y.
sum of(Xi-XMean)*(Yi-YMean) / (sum of (Xi-XMean)^2)
It looks like my for loop does not sum BTak, it only gives me the value for each i. What can I do to have each calculated BTak in a vector and add each value?
I know that i have more things to do after calculating BTak, but I am stuck at this Point.
StressAmp=[200 200 200 200 175 175 175 175 150 150 150 150];
FatigueLife=[9.8E3 1.2E4 4.1E4 2.4E4 7.7E6 5.6E5 4.0E6 5.2E6 2.5E7 9E7 4.2E7 3E7];
%Log
X=log10(StressAmp);
Y=log10(FatigueLife);
%Sort X and Y
[StressAmpSortX,i]=sort(X)
FatigueLifeSortY=(Y(i))
%Mean value X and Y
XMean=mean(X);
YMean=mean(Y);
figure
semilogx(FatigueLife,StressAmp,'*');
ylim([100 250])
grid on
for j=[FatigueLifeSortY]
for k=[StressAmpSortX]
BTak=((k-XMean)*(j-YMean))/((k-XMean)^2)
end
end
Thank you.
0 Comments
Answers (1)
dpb
on 13 Jan 2015
>> S=[200 200 200 200 175 175 175 175 150 150 150 150];
F=[9.8E3 1.2E4 4.1E4 2.4E4 7.7E6 5.6E5 4.0E6 5.2E6 2.5E7 9E7 4.2E7 3E7];
>> S=fliplr(S);F=fliplr(F); % same result as sort given S is already ordered decreasing
>> s=(1:N)-mean(log10(S)); % standardized values
>> f=(1:N)-mean(log10(F));
>> BTak=dot(s,f)/dot(s,s)
BTak =
0.4499
>>
To do it with an explicit sum, you need to use a single loop over 1:length(S) and step thru each array and use a subscript in the LHS assignment. But, Matlab's strength is it's builtin functions that do many of these things innately. Of course, remembering the definition of a dot product is a start plus learning that there is a builtin in function for it takes some time or using the help facilities...Matlab has so much available that it can take a while... :)
2 Comments
dpb
on 13 Jan 2015
Edited: dpb
on 14 Jan 2015
OK, that's a different definition....
NB: that if
SXmean=StressAmpSortX(i)-XMean;
FXmean=FatigueLifeSortY(i)-YMean;
then the summed quantity can be written as
SXmean*FXmean/FXmean^2
which is simply
SXmean/FXmean
Or using shorthand of lowercase s and f to reduce typing at the command line, I get
>> s=StressAmpSortX-XMean;
>> f=FatigueLifeSortY-YMean;
>> dot(1./s,f)
ans =
275.1711
>> sum(BTak)
ans =
275.1711
>>
Again, no loops needed... :)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!