MATLAB Help! MATLAB Code for Rutherford Scattering Won't Run

AI Thread Summary
The discussion centers on troubleshooting a MATLAB code for simulating Rutherford scattering, specifically the trajectory of alpha particles. The user encounters an error indicating an attempt to access an out-of-bounds index in the array a_x, which only contains one element. The error arises from a for loop that tries to access elements a_x(2) and beyond without assigning values to them. Suggestions include simplifying variable usage, such as using velocity_x directly instead of v_x, and manually simulating iterations of the loop to better understand the code's logic. The overall consensus is that while the code has potential, it requires significant adjustments to run successfully.
allybally
Messages
2
Reaction score
0
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

(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

Last edited by a moderator:
Physics news on Phys.org


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.
 


shoehorn said:
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?
 


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
 

Similar threads

Back
Top