Filter löschen
Filter löschen

Using the right values for the variables, function gives a completely wrong answer

2 Ansichten (letzte 30 Tage)
Hello,
I'm afraid of this being a rather simple solution, but I am out of ideas here.. I've been setting up a calcuation in matlab, and I spotted a function giving a completely wrong answer despite of all variables being correct:
clear all; close all;
%%
% Constants used for this calculation
g=9.81; % [m/s2] gravitational constant
rho_w=1000; % [kg/m3] water density
nu=10^-6; % [m2/s] water kinematic viscosity
k=0.4; % [-] Von Karman constant
% Additionally considering the following:
T=6; % [s] wave period
H=1.2; % [m] wave height
D=20; % [m] water depth
%% Problem 2.1
% Calculating wavenumber, wavelength, and celerity at this depth:
syms k
omega = 2*pi/T
omega = 1.0472
Linear = omega^2 == g*k*tanh(k*D)
Linear = 
k_initial = omega^2/g % using deep water wave number was an initial condition
k_initial = 0.1118
sol_k=vpasolve(Linear,k,k_initial)
sol_k = 
0.11413692045541882681977686977527
L=2*pi/sol_k
L = 
My issue is specifically with the very last line here. When checking each variable up to that point one by one, it is clear that pi=3.1416, sol_k=0.1141369, which should give 2*3.1416/0.1141369=55.05. Instead matlab is telling me it's 17.523.
I've been sitting here for some time and I am confused on how this can even be possible here, considering it is a simple division.
  1 Kommentar
Stephen23
Stephen23 am 3 Dez. 2022
"Instead matlab is telling me it's 17.523. "
No, MATLAB is telling you that it is 17.523* pi:
If you want a purely numeric value without pi, then use DOUBLE(L)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 3 Dez. 2022
Verschoben: Image Analyst am 3 Dez. 2022
You forgot that 17.523 is multiplied by pi in the calculation of L.
  4 Kommentare
Torsten
Torsten am 3 Dez. 2022
Use
sol_k = double(vpasolve(Linear,k,k_initial))
instead of
sol_k = vpasolve(Linear,k,k_initial)
Then you won't have trouble with long numbers and pi's.
Steven Lord
Steven Lord am 4 Dez. 2022
is there a reason why should it add a pi at the very end instead of multiplying with it, when asked?
You told MATLAB to perform the calculations symbolically so it gave you a symbolic answer rather than returning a double precision approximation to that symbolic value. As others have said, if you want to see the double precision approximation call double on the result or you could use sympref to change how MATLAB displays the symbolic expression.
two = sym(2)
two = 
2
root2pi = sqrt(two)*pi
root2pi = 
sympref('FloatingPointOutput', true); % The default is false
root2pi_num = sqrt(two)*pi
root2pi_num = 
4.4429
d = double(root2pi)
d = 4.4429

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by