Jump in calculation results

1 view (last 30 days)
Christoph R
Christoph R on 15 Aug 2019
Commented: Guillaume on 15 Aug 2019
A mistake appears in the results of the following calculation.
for i=1:Anzahl
for j=1:MaxDateienGroesse
a_Korrekt_fK(j,i) = (a_fK(j,i)+Delta_a_Anfang_fK(1,i))+((a_fK(j,1)-...
a_Anfang_fK(1,i))/(a_Ende_fK(1,i)-a_Anfang_fK(1,i)))*Delta_a_Ges_fK(1,i);
a_Korrekt_sK(j,i) = (a_sK(j,i)+Delta_a_Anfang_sK(1,i))+((a_sK(j,1)-...
a_Anfang_sK(1,i))/(a_Ende_sK(1,i)-a_Anfang_sK(1,i)))*Delta_a_Ges_sK(1,i);
if i==2
Var(j,:)=[j,a_fK(j,i),Delta_a_Anfang_fK(1,i),a_Anfang_fK(1,i),a_Ende_fK(1,i),Delta_a_Ges_fK(1,i),a_Korrekt_fK(j,i)];
end
end
end
For j=6100 the value of a_Korrekt_fK decreases from 0,023 to 0,018 but only for i=2. This behavior is consistent with the use of Array Indexing.
a_Korrekt_fK(:,i) = (a_fK(:,i)+Delta_a_Anfang_fK(1,i))+((a_fK(:,1)-...
a_Anfang_fK(1,i))/(a_Ende_fK(1,i)-a_Anfang_fK(1,i)))*Delta_a_Ges_fK(1,i);
In the first code, "Var" was used to print all values to Excel in order to check their correctness. Therefore the calculation for a_Korrekt_fK was repeated in Excel. and the results plotted vs j as shown in the following image. The Excel file is also added.
Image.bmp
Now I have no idea how I can solve this issue and hope for your kind assistance.
  1 Comment
Star Strider
Star Strider on 15 Aug 2019
Please describe what you want your code to do, and what your code is doing that you do not want it to do.
... check their correctness.
What defines ‘correctness’?

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 15 Aug 2019
I'm unclear what you're trying to do with your double for loop. You haven't told us what Anzahl and MaxDateienGroesse stand for, nor have you explained how you go from your loop to the a_Korrekt_Matlab column of your excel file.
If the intent is to have that a_Korrekt_Matlab match the a_Korrekt-Excel then I certainly don't understand what columns you're looping over since everything is column vectors. It's also unclear why for some of the variables you're only using the first column in your formula.
To reproduce the excel calculation which matches the formula in your question it's simply:
t = readtable('Var.xlsx'); %will warn about making variable names valid
a_Korrekt_fK = t.a_fK + t.Delta_a_Anfang_fK + (t.a_fK - t.a_Anfang_fK) ./ (t.a_Ende_fK - t.a_Anfang_fK) .* t.Delta_a_Ges_fK;
If you compare that against the excel data, it's a complete match
>> all(abs(a_Korrekt_fK - t.a_Korrekt_Excel) <= 1e-15) %proper way to compare floating point numbers
ans =
logical
1
  4 Comments
Guillaume
Guillaume on 15 Aug 2019
Christoph R's comment mistakenly posted as an answer moved here:
The values a_fK and a_sK are crack lengths measured during the experiment. They contain a systematic error in the measurement which has to be eliminated. The resulting values are a_Korrekt_fK and a_Korrekt_sK.
Var was only created to examine the root of the error and has no further use to the code. The loops are used because I am not an expert in matlab.
The Inputs are quite large Excel-Files containing both numeric and non-numeric values. Therfore I am using importdata. In the code a_fK is the crack length at any given time of the experiment. a_Anfang and. a_Ende are the crack lengths at the beginning respectively end of the experiment. Delta_a_anfang and Delta_a-Ges are correction values that stay the same for the entire set of measured crack lengths a_fK.
What still bothers me is the fact that the calculation works if I combine alle the necessary values for the calculation first.
for i=1:Anzahl
for j=1:MaxDateienGroesse
Var1 = [a_fK(j,i),Delta_a_Anfang_fK(1,i),a_Anfang_fK(1,i),a_Ende_fK(1,i),Delta_a_Ges_fK(1,i)];
a_Korrekt_fK(j,i) = Var1(1) + Var1(2) + (Var1(1) - Var1(3)) ./ (Var1(4) - Var1(3)) .* Var1(5);
Var2 = [a_sK(j,i),Delta_a_Anfang_sK(1,i),a_Anfang_sK(1,i),a_Ende_sK(1,i),Delta_a_Ges_sK(1,i)];
a_Korrekt_SK(j,i) = Var2(1) + Var2(2) + (Var2(1) - Var2(3)) ./ (Var2(4) - Var2(3)) .* Var2(5);
end
end
But as soon as I go the direct way as shown in the beginning, at some point (in this case line 6100) matlab seems to decide that it dows not want to calculate rationally.
Guillaume
Guillaume on 15 Aug 2019
matlab seems to decide that it dows not want to calculate rationally.
Matlab doesn't decide to do anything. If it doesn't produce the result you want, it's because of a bug in your code or in your assumptions.
I'm still not very clear on what the input data is, in particular what's a vector and what's a matrix. Assuming:
  • a_?K are matrices with Anzahl columns.
  • Delta_a_Anfang_?K, a_Anfang_?K, a_Ende_*K, and Delta_a_Ges_*K are row vectors of size 1 x Anzahl
  • you're on matlab >= R2016b
assert(isrow(Delta_a_Anfang_fK) & isrow(a_Anfang_fK) & isrow(a_Ende_fK) & isrow(Delta_a_Ges_fK), 'At least one of the vectors is not a row vector');
assert(all(diff([numel(Delta_a_Anfang_fK), numel(a_Anfang_fK), numel(a_Ende_fK), numel(Delta_a_Ges_fK), size(a_fK, 2)]) == 0), 'Sizes don''t match');
then the equivalent of the loop above is:
a_Korrekt_fK = a_fK + Delta_a_Anfang_fK + (a_fK - a_Anfang_fK) ./ (a_Ende_fK - a_Anfang_fK) .* Delta_a_Ges_fK;
Same for the *_sk variables.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!