# What's wrong with this MATLAB code for Rutherford scattering, it won't run for me!

• MATLAB
What's wrong with this MATLAB code for Rutherford scattering, it won't run for me! :(

Hi,
I can't do matlab...and have to plot the trajectory of alpha particles in rutherford scattering. I've made an attempt, but i know i've gone wrong somewhere. Could someone tell me how to fix it? I'd be SOOOO thankful...you've no idea!!
This is what i have so far, i've added it as an attachment as an m file.

I want it to look something like this:
http://www.kutl.kyushu-u.ac.jp/seminar/MicroWorld1_E/Part2_E/P25_E/Rutherford_S_E.jpg [Broken]

(and know the way i have it, there should only be one trajectory...i just want it to run! then i'll do it for other impact parameters too.

Thank you so much in advance!

#### Attachments

• 955 bytes Views: 395
Last edited by a moderator:

I haven't bothered to check the logic of your routine since, well, it's fugly. However, Matlab tells you precisely why your script won't run:

Code:
??? Attempted to access a_x(2); index out of bounds because numel(a_x)=1.

I haven't bothered to check the logic of your routine since, well, it's fugly. However, Matlab tells you precisely why your script won't run:

Code:
??? Attempted to access a_x(2); index out of bounds because numel(a_x)=1.
Is it that bad?? would i get any marks for it do you think?? I'm stressing, i don't have a clue whatto do!

Yeah, that's the error that came up,but i don't know how to fix it....do you???

Mark44
Mentor

The code doesn't look all that bad to me, but it definitely has problems. The variables have names that make it possible to guess what they should contain, and that's a plus, but you might have more variables than you need, which makes understanding the code more difficult.

Your problem is that you have an array a_x with 1 defined element, but in your for loop you are trying to read from a_x(2), a_x(3), etc., but there is no place in your code where you are giving values to these variables.

The first line in your for loop sets velocity_x(2) with the values in v_x(1) and a_x(2) and a_x(1). When do you assign a value to a_x(2)? Do you really need a variable v_x? Why not just use velocity_x instead?

Since you presumably wrote the for loop, you need to understand what it is doing, and you can do that by hand-simulating what the computer will do for an iteration or two at least. Start off with i = 1, and calculate the values that will be stored in velocity_x(2), position_x(2), velocity_y(2), and position_y(2). Doing this should increase your understanding of why you're getting the error you are getting.

A precondition for your for loop is that