matrix calculation have wrong answer

I have the following code,
clear;
time = datestr(now,'dd-mm-yyyy HH-MM-SS');
input=xlsread('Book1.xlsx');
load('beta0_1.mat');
load('kbeta_bao_hoa.mat');
load('x_delta.mat');
load('kI');
load('Ka');
Z1 = 48;
Z2 = 40;
x = [221.1321 336.6439 0.6575 5.3102 345.8133 7.6177 9.5410 19.3115 0.7990 8.8932 17.1360 8.8941 8.8954 30.9960 2.0003 ];
y_test = linspace(0.5,4,100);
z_test = linspace(1, 10, 100);
[X,Y] = meshgrid(y_test,z_test);
m_k = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,X,Y);
surf(X,Y,m_k);
hold on;
m_k_x = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,1.419,5.545);
plot3(0.7990,8.8932,m_k_x,'.r','markersize',10);
m_k_ranhTQ_1 is a function with 1 ouput. When i run this script, m_k is a 100x100 matrix. The problem is the result in the m_k matrix have wrong answers. Like you can see in the script, i try to highlight the point (1.419; 5.545) on the surface of the 3d graph, but it is not even on the surface, it is way off. I try to go to the Workspace to check manually, and typing in the command window like:
m_k_x = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,X(100,100),Y(100,100))
and compare it with m_k (100,100), and it is not the same answer. I think m_k (x,y) = f[X(x,y); Y(x,y)]. I check and if i use low index, like
m_k_x = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,X(30,16),Y(30,16))
In this case, m_k_x = m_k(30, 16)
Can someone tell me where i did wrong and how can i fix this?

9 comentarios

Bjorn Gustavsson
Bjorn Gustavsson el 20 de Feb. de 2022
How could we possibly? You will have to show the details of your m_k_ranhTQ_1 function for us to be able to help.
quan ng
quan ng el 20 de Feb. de 2022
I have attach all files needed to run the above script
Catalytic
Catalytic el 20 de Feb. de 2022
I have attach all files needed to run the above script
Why so many? Why not put everything in one .mat file so it's easier for us to download?
quan ng
quan ng el 20 de Feb. de 2022
sorry, i use grab it to create it from image file, and i just left it to use
quan ng
quan ng el 20 de Feb. de 2022
i now use loop to calculate the m_k matrix:
count_i = 0;
count_j = 0;
for i = y_test
count_i = count_i + 1;
if count_j == 100
count_j = 0;
end
for j = z_test
count_j = count_j + 1;
m_k(count_i,count_j) = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,i,j);
end
end
surf(X,Y,m_k);
quan ng
quan ng el 20 de Feb. de 2022
Editada: quan ng el 20 de Feb. de 2022
now the m_k matrix is right (i checked it), but the surf is wrong
i use:
surf(X,Y,m_k);
hold on;
m_k_x = - m_k_ranhTQ_1(x,input,beta0,kbeta_bao_hoa,x_delta,kI,Ka,time,Z1,Z2,0.799,8.8932);
plot3(0.7990,8.8932,m_k_x,'.r','markersize',10);
and it is still not on the surface
I attach the graph for you to see
Bjorn Gustavsson
Bjorn Gustavsson el 20 de Feb. de 2022
Another point you might consider is to write your code in "english".
It might seem a bit rude of me to suggest that your native tounge is not suitabel - but that's not what I insinuate. Once uppon a time I programmed in my native language, function and variable-names and comments. Then a colleague that did not speak my language tried to use/port the programme, which made me feel very rude. Since then I've stuck to working in english - even if neither of me nor my colleagues have it as a first language we can get by...
quan ng
quan ng el 20 de Feb. de 2022
thanks for your feedbak, next time i will try. Quick question though, is the "surf" function somehow try to alter the result to smooth the surface or something? I try surf and have values that is not in the matrix. I try to plot each points using loops and it is correct, while the graph of the surf function is not. I will attach the image of the point by point graph, you can compare with the surf.png i attach above. The red dot is the point i use to check if it is on the surface. If you can take a quick look, i will be gratefull.
quan ng
quan ng el 20 de Feb. de 2022
Editada: quan ng el 20 de Feb. de 2022
sorry i have to delete the .mat files so i can upload other question with attached files. Mathworks limit 10 files daily upload. If you can answer my question about the surf function, please go to:
https://www.mathworks.com/matlabcentral/answers/1654295-surf-function-give-wrong-graph

Iniciar sesión para comentar.

 Respuesta aceptada

Voss
Voss el 20 de Feb. de 2022
Editada: Voss el 20 de Feb. de 2022
I think the difference you see between the case when you run m_k_ranhTQ_1() with matrix X,Y vs the case when you run it with scalar X,Y in for loops, is due to the logic near the bottom of m_k_ranhTQ_1.m here:
if h_px > (h1_r + h2_r)
% ...
end
if h_px<=(h1_r + h2_r)
% ...
end
When you send scalar X,Y in, then those lines are ok (if redundant), but when you send matrix X,Y, then h_px and h1_r are both matrices. Using a matrix conditional in an if statement evaluates to true only if the condition is true for all elements of the matrix. Therefore you get different results when you send in the whole matrix vs when you send the elements one at a time.
The solution is to adapt your function m_k_ranhTQ_1() to handle matrix inputs correctly (if you want to use it like that).

3 comentarios

quan ng
quan ng el 21 de Feb. de 2022
yes, i will try that. For now i think i will use the loops to calculate the m_k matrix. And you answer my surf question too! You have been a great help!
Voss
Voss el 21 de Feb. de 2022
I'm glad to help! And thanks for accepting this answer too!
quan ng
quan ng el 21 de Feb. de 2022
No problem! Thank you too!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 20 de Feb. de 2022

Comentada:

el 21 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by