When calling a user defined function, MATLAB throws an error for simple matrix multiplication
Ältere Kommentare anzeigen
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);
function [B] = planck_function(A, X)
global const1 const2
B = A(2).*(const1./(X.^5)) ./ (exp(const2./(A(1).*X))-1);
end
Akzeptierte Antwort
Weitere Antworten (2)
Jan
am 21 Sep. 2022
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.
"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".
Kategorien
Mehr zu Computational Fluid Dynamics (CFD) finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!