Effacer les filtres
Effacer les filtres

Matlab - create a table from a vector in the first row and another in the first column

3 vues (au cours des 30 derniers jours)
Hello, I am trying to make a table inside Matlab, but it is not coming out with the correct results.
My routine:
alpha=0.008;
gamma=3.3;
g=9.80665;
delta_1=0.07; % if f<=fp
delta_2=0.09; % if f>fp
Tp=2:16;
fp=1./Tp;
T=2:16;
f=1./T;
nrows = 14;
ncols = 14;
E = ones(nrows,ncols);
for fp = 1:ncols
for f = 1:nrows
if f<=fp
E(f,fp) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
elseif f>fp
E(f,fp) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
else
E(f,fp) = 0;
end
end
end
E
Attached is a figure that is reality (as it turned out) and another as expectation (as it was meant to be).
I thank you for your help.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 6 Oct 2020
hello
i noticed some mistakes regarding usage of f and fp : you have initialised as vectors of decimal scalars. Then later on, you use them as loop index , therefore overwritting the f and fp values as integers. I guess this was not the intention.
i could not get the expected result because I don't know how the reference (expected) data where generated. i miss this info.
What is sure, is that the row / columns where we expect to see 0 cannot be obtained with your matlab code. So there must be another error somewhere in the logic and / or in the equations.
if you could send me the math behind what you are trying to do, I could better help you.
FYI, this is how I modified your code in the first place
clc
clear all
%%%%%%%%%%
alpha=0.008;
gamma=3.3;
g=9.80665;
delta_1=0.07; % if f<=fp
delta_2=0.09; % if f>fp
Tp=2:16;
% fp=1./Tp;
fp_vector=1./Tp; %
T=2:16;
% f=1./T;
f_vector=1./T;
nrows = 14;
ncols = 14;
E = ones(nrows,ncols);
% ?? f and fp cannot be at the same time used as vector of decimal scalars and index in a for loop
% see correction below
% for fp = 1:ncols % fp loop
for cj = 1:ncols % fp loop
% for f = 1:nrows % f loop
for ci = 1:nrows % f loop
% extract f and fp value
f = f_vector(ci);
fp = fp_vector(cj);
if f<=fp
E(ci,cj) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
elseif f>fp
E(ci,cj) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
else
E(ci,cj) = 0
% this condition is never reached as all cases are already covered with the two conditions above
% so what condition is supposed to give E = 0 ?
end
end
end
E
  2 commentaires
Jacqueline Rigatto
Jacqueline Rigatto le 7 Oct 2020
Modifié(e) : Jacqueline Rigatto le 7 Oct 2020
Thank you so much for your help, Mathieu NOE.
Mathieu NOE
Mathieu NOE le 7 Oct 2020
happy to have helped someone today !!

Connectez-vous pour commenter.

Plus de réponses (0)

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