matrix calculation have wrong answer

3 vues (au cours des 30 derniers jours)
quan ng
quan ng le 20 Fév 2022
Commenté : quan ng le 21 Fév 2022
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 commentaires
quan ng
quan ng le 20 Fév 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 le 20 Fév 2022
Modifié(e) : quan ng le 20 Fév 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

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 20 Fév 2022
Modifié(e) : Voss le 20 Fév 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 commentaires
Voss
Voss le 21 Fév 2022
I'm glad to help! And thanks for accepting this answer too!
quan ng
quan ng le 21 Fév 2022
No problem! Thank you too!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Geometric Transformation and Image Registration 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