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 Comments

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.
I have attach all files needed to run the above script
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?
sorry, i use grab it to create it from image file, and i just left it to use
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 on 20 Feb 2022
Edited: quan ng on 20 Feb 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
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...
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 on 20 Feb 2022
Edited: quan ng on 20 Feb 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

Sign in to comment.

 Accepted Answer

Voss
Voss on 20 Feb 2022
Edited: Voss on 20 Feb 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 Comments

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!
I'm glad to help! And thanks for accepting this answer too!
No problem! Thank you too!

Sign in to comment.

More Answers (0)

Asked:

on 20 Feb 2022

Commented:

on 21 Feb 2022

Community Treasure Hunt

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

Start Hunting!