Reputation: 651
tk=linspace(0,1,101);
R = 35.9; L = 3.98; C = 0.000360; P = ceil(R/(2*L));
M = ceil(sqrt( (1/(L*C)) - ( (R^2)/ (4*(L^2)) )));
syms q(t);
syms i(t);
q(t) = 40*exp((-R/(2*L))*t).*cos(t*sqrt((1/(L*C))-((R^2)/(4*L^2))));
i(t) = diff(q);
exact_intensity_values = 1:101;
aux = 1:101;
for ix = 1:101
exact_intensity_values(ix) = i(tk(ix));
disp('exact intensity value');
disp(exact_intensity_values(ix));
aux(ix) = (exact_intensity_values(ix))^(-1/(P * tk(ix)));
disp('aux value exact');
disp((aux(ix))^(-P * tk(ix)));
end
So, sometimes the two values displayed are the same, as it is supposed to be, but sometimes they are very different. What am I doing wrong?
Here are some examples, I won't post all of them. Usually when the exact intensity value is negative the aux value is its symmetric, as in the same absolute value but positive. Other times the numbers have nothing to do with each other. Other times they are the same. I'm completely lost, I've been debugging this for a long time now.
This one is for tk(ix) = 0. It makes no sense whatsoever.
exact intensity value -180.4020
aux value exact 1
Edit - Now that I think about it a bit more it probably makes sense, since tk(1) = 0 we have exact_intensity_values(ix)^(-1/0)
, and that probably causes the problems here. This can probably be safely ignored.
These ones have the correct absolute value, but incorrect sign.
exact intensity value -422.8061
aux value exact 422.8061
exact intensity value -616.2485
aux value exact 616.2485
These ones are correct: exact intensity value 464.3460
aux value exact 464.3460
exact intensity value 412.2708
aux value exact 412.2708
exact intensity value 337.3326
aux value exact 337.3326
exact intensity value 246.4757
aux value exact 246.4757
These ones have a negative exact value but are correct:
exact intensity value -48.4391
aux value exact -48.4391 + 0.0000i
exact intensity value -132.0104
aux value exact -1.3201e+02 + 2.2673e-14i
exact intensity value -199.6144
aux value exact -1.9961e+02 + 6.9122e-14i
I can't see any pattern in what causes some values to be correct and others incorrect, it seems absolutely random. I've tried both ^ and the power function but the results are exactly the same...
MATLAB Version: 9.5.0.944444 (R2018b)
Edit #2: So the values are only incorrect for the first values of the curve. Here are the graphs, the first one has all the correct values and the second one has the incorrect values as well as some correct.
Upvotes: 3
Views: 102
Reputation: 24169
I know you said that you solved this problem, but I'm providing this answer so that the question doesn't remain unanswered.
It appears that the "very different" values simply have a small imaginary component. Otherwise, the real part appears to be the same (within numerical error), and only the notation is different (which is likely the default display format for complex doubles):
-1.3201e+02
likely means the same as -132.0104
.-1.9961e+02
likely means the same as -199.6144
.0.0000i
, 2.2673e-14i
and 6.9122e-14i
mean 0
practically, but not numerically.I would advise to surround values that should be real with real()
.
See also: Why is 24.0000 not equal to 24.0000 in MATLAB?
Upvotes: 1