When calling a user defined function, MATLAB throws an error for simple matrix multiplication

2 ビュー (過去 30 日間)
I have created a function to do the planckbody distribution in order top use it in the 'lsqcurvefit' function. excpet when I run the fucntion i get an incompatible array error. except it should be a scalar multiplying an array... so there should be no problem.
And the best part is that it runs on other people's computers
But when I run "B" line in the main body of the code I it runs fine. What is going on here?
%%%% Planck fit for T_surf
%%%% J. Meyers, O. Plante
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
close all
clc
set(0,'DefaultFigureWindowStyle','docked')
%global const1 const2 lambda % !!! Does this only need to be defined as
%global within the function???
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Emission_data = textread('Calibrated_Surface_Emission.txt'); %Lambda 1 color (1690 K) 2 color (1750 K)
lambda = Emission_data(:,1);
E1 = Emission_data(:,2);
E2 = Emission_data(:,3);
Emission_data_trim = textread('Calibrated_Surface_Emission_Trim1.txt');
lambda_trim = Emission_data_trim(:,1);
E1_trim = Emission_data_trim(:,2);
E2_trim = Emission_data_trim(:,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Create Planck Curves
h = 6.62606896*10^-34; %[J-sec]
c = 2.99792458*10^17; %[nm/sec]
k = 1.3806504*10^-23; %[J/K]
const1 = 2*h*c^2; %[J-nm^2/sec]
const2 = h*c/k; %[nm-K]
%%%
T = 1700; % [K]
B_Scale = 3.8e17;
A = [T B_Scale];
%B1 = A(2).*(const1./(lambda.^5)) ./ (exp(const2./(A(1).*lambda))-1)
[B1] = planck_function(A,lambda);
Error using ./
Arrays have incompatible sizes for this operation.

Error in solution>planck_function (line 39)
B = A(2).*(const1./(X.^5)) ./ (exp(const2./(A(1).*X))-1);
function [B] = planck_function(A, X)
global const1 const2
B = A(2).*(const1./(X.^5)) ./ (exp(const2./(A(1).*X))-1);
end

採用された回答

Star Strider
Star Strider 2022 年 9 月 21 日
It is best to not use global variables. The problem is that they have to be defined in the calling routine as well as the functions that use them. Pass the parameters as extra parameters instead. If there are a lot of them, save them in a structure or cell array, and pass that as an extra parameter. See: Passing Extra Parameters for an extended discussion.
%%%% Planck fit for T_surf
%%%% J. Meyers, O. Plante
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(0,'DefaultFigureWindowStyle','docked')
%global const1 const2 lambda % !!! Does this only need to be defined as
%global within the function???
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Emission_data = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132130/Calibrated_Surface_Emission.txt'); %Lambda 1 color (1690 K) 2 color (1750 K)
lambda = Emission_data(:,1);
E1 = Emission_data(:,2);
E2 = Emission_data(:,3);
Emission_data_trim = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1132135/Calibrated_Surface_Emission_Trim1.txt');
lambda_trim = Emission_data_trim(:,1);
E1_trim = Emission_data_trim(:,2);
E2_trim = Emission_data_trim(:,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Create Planck Curves
h = 6.62606896*10^-34; %[J-sec]
c = 2.99792458*10^17; %[nm/sec]
k = 1.3806504*10^-23; %[J/K]
const1 = 2*h*c^2; %[J-nm^2/sec]
const2 = h*c/k; %[nm-K]
%%%
T = 1700; % [K]
B_Scale = 3.8e17;
A = [T B_Scale];
%B1 = A(2).*(const1./(lambda.^5)) ./ (exp(const2./(A(1).*lambda))-1)
[B1] = planck_function(A,lambda, const1, const2);
B1 = 3648×1
1.0e+00 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
function [B] = planck_function(A, X, const1, const2)
% global const1 const2
B = A(2).*(const1./(X.^5)) ./ (exp(const2./(A(1).*X))-1);
end
This now runs wtihout error.
.

その他の回答 (2 件)

Jan
Jan 2022 年 9 月 21 日
Use the debugger to examine the problem:
dbstop if error
Run your code again afterwards. If it stops at the error, check the dimensions of the arguments:
size(A(2))
size(const1)
size(X)
size(exp(const2 ./ (A(1).*X)) - 1)
Which of the 3 divisions is the problem?
a = const1 ./ (X.^5)
b = const2 ./ (A(1).*X)
c = A(2).*(a) ./ (exp(b)-1);
I assume, the problem is hidden in global variables const1, const2. Remember that itis hard to control, where global variables with such simple names are overwritten. At least the posted code does not contain the definition. Because global variables cause such problems frequently and impede the debugging, it is recommended to avoid them strictly.

Torsten
Torsten 2022 年 9 月 21 日
編集済み: Torsten 2022 年 9 月 21 日
"const1" and "const2" are not visible in "planck_function" since you didn't declare them as "global" in the calling program.
But as a general advice, you should try to avoid global variables. You should directly use them as input to "planck_function".

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by