How to describe a function with an if statement and then integrate it.

Hello,
I am trying to describe a function pretty peculiar and then I need to integrate it. The problem comes in a few ways, and every attend i try result on fail. I allready tried syms variables, anonimous function and "normal fuction" (those defined as-> funtcion [output]=name(input) ..... end).
The context: i am trying to sampling a distribute function, more precisly the diferencial crossection of the rayleight scattering (photons with matter stuff). In some point i have to figure out an angle "theta", or its cosine, its the same.
Inputs: given a Energy ->E , an atomic number ->Z . I suggest to try with E = 1 000 000 (eV), and Z = 11.
Outputs: I need "theta", "E_nueva" should be the same number as E.
My best attempt was:
E = 1e6; Z = 11;
[sigma_rayleigh_valor] = sigma_rayleigh(E,Z)
Conversion to logical from sym is not possible.

Error in solution>sigma_rayleigh (line 33)
if (Z>10) && (f<2) % <-------- Here is where the error happens
function [sigma_rayleigh_valor] = sigma_rayleigh(E,Z)
% Esta funcion devuelve el valor de la seccion eficaz rayleigh para
% una energia dada E y una valor del numero atomico Z.
re = 2.8179e-13; % [cm] Radio del electron clasico. Is a number
% Hay que definir el factor de forma F:
% Definimos la funcion F(x,z):
Tabla1 = readtable("https://de.mathworks.com/matlabcentral/answers/uploaded_files/1214213/Tabla1.xlsx");
a1 = Tabla1{Z,2}; % a number, try for example "8"
a2 = Tabla1{Z,3}; % a number, try for example "7"
a3 = Tabla1{Z,4}; % a number, try for example "6"
a4 = Tabla1{Z,5}; % a number, try for example "5"
a5 = Tabla1{Z,6}; % a number, try for example "4"
syms costheta
x = 20.6074*(E/511000)*(2*(1-costheta))^(1/2); % x=x(E,costheta)
f = Z*(1+a1*x^2+a2*x^(3)+a3*x^4)/((1+a4*x^2+a5*x^4)^2); % f=f(x)=f(Z,E,costheta)
a = (Z-5/16)*(1/137); % a=a(Z)
gamma = sqrt(1-a^2); % gamma=gamma(a)=gamma(Z)
Q = x/(2*a*20.6074); % Q=Q(x,a)=Q(Z,E,costheta)
Fk = sin(2*(gamma)*atan(Q))/((gamma)*(Q)*(1+(Q)^2)^(gamma)); % Fk = Fk(gamma,Q) = ... = Fk(Z,E,costheta)
if (Z>10) && (f<2) % <-------- Here is where the error happens
F = max([f Fk]);
else
F = f;
end
% El factor de forma F ha quedado descrito.
fun = (1+costheta^2)*(F)^2; % Este es el integrando.
sigma_rayleigh_valor = pi*re^2*int(fun,costheta,-1,1);
sigma_rayleigh_valor = double(sigma_rayleigh_valor); % Con la funcion "double" paso de tener una variable syms a un escalar tipo double.
end
I need to clarify: With Z <= 10 there is no problem at all... the code seems to report a number, not an error ^^". When Z>10 the problem occurs

4 Comments

We don't have Tabla1, so we cannot test your code.
Uploaded, thanks for taking your time.
f cannot be evaluated to be < 2 or not since it contains an unknown symbolic parameter "costheta".
Well, i understand what the problem is. Is there any way to code the function "F" properly?

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 30 Nov 2022

Edited:

on 1 Dec 2022

Community Treasure Hunt

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

Start Hunting!