Jump in calculation results

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

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)

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

Thank you for your response.
With this code I want to evaluate and plot experimental results of multiple specimen. In order to do so the values a_fK and a_sK have to be adjusted to a_Korrekt_fK and a_Korrekt_sK.
The first loop performs the calculation for all specimen data sheets imported at an earlier stage of the code. Anzahl itself is the number of specimen (4).
MaxDateienGroesse is a value that is defined by the user (35000). It is used to fill the imported data with zeros in order for all of them to have the same size. The second loop was used to check if there is a mistake within the Array Indexing.
As you correctly stated, the values of a_Korrekt-Excel are the expected results whereas a_Korrekt_Matlab are the values that are calculated by matlab. I used xlswrite to export the values of a_Korrekt_fK to Excel where I named them a_Korrekt_Matlab.
Following your suggestion and trying readtable('Var.xlsx') I now am compleatly confused because the values for a_Korrekt_fK calculated afterwards indeed match the correct values in excel. However Var itself still shows the aforementioned error from line 6100 on.
the values a_fK and a_sK have to be adjusted to a_Korrekt_fK and a_Korrekt_sK
Sorry, I don't understand what that means.
As far as I can tell, no loop is needed to do whatever is doing. As I've shown, for column vectors, it can be done in just one line. The code I've used would work with matrices as well (as long as they're of compatible size).
readtable is just an easy way to import your data. The code I've written doesn't create or modify any variable called Var, so if it existed before, it would not have changed.
It may be easier if you gave an example of what the true inputs actually are.
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.
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 Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 15 Aug 2019

Commented:

on 15 Aug 2019

Community Treasure Hunt

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

Start Hunting!