Asked by Clifton Whittaker
on 19 Oct 2019

Hello, I am trying to create a matlab script that calculates an atomic scattering factor dependent on a,b,s,z where z is the atomic number, a and b are a 4 term array I will be getting from my course textbook, and s is sin(theta)/lambda which I will be calculating by hand as part of my homework. Not sure why I am having this difficulty but it seems my matlab is much ruster than I thought, but I would rather do this than an excel spreadsheet.

Equation should look something like:

My code currently looks like this:

function f = f(z,s,a,b)

f =(z^2)-41.78214*(s.^2)*symsum(a.*(exp(1).^-b.*(s.^2)),[a,b],[1 4]);

end

Varaible are curently:

When I remove the summation I get an answer (woohoo!), but when i try to use the summation it really gives me the error:

>> Scatteringfactor(z,s,a,b);

Error: File: Scatteringfactor.m Line: 3 Column: 67

Unbalanced or unexpected parenthesis or bracket.

I suspect I am misusing the symsum function or there is a smarter way of doing this.

Answer by Clifton Whittaker
on 19 Oct 2019

Sign in to comment.

Answer by Steven Lord
on 19 Oct 2019

There are no symbolic variables in the function you posted, so symsum isn't the right tool for this job. Instead, just use sum.

x = 1:10;

y = sum(x)

You also should use exp(x) rather than exp(1).^x.

Clifton Whittaker
on 19 Oct 2019

Ok so I replaced symsum with sum and now have the following:

function f = f(z,s,a,b)

f =(z)-41.78214*(s^2)*sum(a*(exp(1).^-b*(s.^2)),[a,b]);

end

Now I am being told that the inner matrix dimension do not match?

both a and b are a 4 element array and z and s are defined.

Is this still issues with my sum function?

Guillaume
on 19 Oct 2019

Is s scalar or a matrix? In the same function you have both s^2 and s.^2. If s is a square matrix, the two will produce very different results. Of course, if s is a non-square matrix, s^2 is an error (but s.^2 is fine). For scalars there's no difference between the two but it's very sloppy to mix the two notations.

Similarly, if a, b and s are vectors/matrices, it's not clear if you meant to use matrix multiplication * or memberwise multiplication .*.

Mix up between .^ and ^ and between .* and * are most likely the reason for the error.

The brackets around the (z) are also puzzling. They don't do anything useful.

Finally, it's not clear why you're passing [a, b] as a 2nd input to sum. The second input of sum is the dimension to operate along. I strongly suspect that neither a or b are valid dimension (i.e. scalar positive integers).

Sign in to comment.

Answer by Guillaume
on 19 Oct 2019

function scatterfactor = calculatescatteringfactor(Z, s, a, b)

assert(numel(s) == 1, 's must be scalar');

assert(isequal(size(a), size(b)), 'a and b must be the same size');

assert(numel(Z) == 1, 'Z must be scalar');

scatterfactor = Z - 41.78214 * s^2 * sum(a .* exp(-b .* s^2));

end

is the correct syntax for this function.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.