Help with For loop during least square fit

1 vue (au cours des 30 derniers jours)
Daniel
Daniel le 13 Jan 2015
Modifié(e) : dpb le 14 Jan 2015
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.

Réponses (1)

dpb
dpb le 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 commentaires
Daniel
Daniel le 13 Jan 2015
I got s and f to be of different number of columns?... I tried this instead
for i=1:length(StressAmpSortX)
BTak(i)=((StressAmpSortX(i)-XMean)*(FatigueLifeSortY(i)-YMean))/((StressAmpSortX(i)-XMean)^2)
end
BTak=sum(Btak)
and it seems to what I want.
I agree, there are many builtin fuctions, I found out that polyfit just did all my work... Thanks
dpb
dpb le 13 Jan 2015
Modifié(e) : dpb le 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... :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by