Help! MATLAB Code for Rutherford Scattering Won't Run

In summary: This tells the for loop that a_x will have a value between 1 and the size of a_x. You might be wondering why you need to set a_x at all if you are only going to be using it once. The reason is that the for loop is going to initialize all the other variables in the for loop as well. If you don't initialize a_x, then the for loop will try to use the value 1 for a_x, which will result in an error. In summary, you have a for loop that is trying to access a_x(2), which doesn't exist because you defined a_x at the beginning as having
  • #1
allybally
2
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

  • project.m
    955 bytes · Views: 499
Last edited by a moderator:
Physics news on Phys.org
  • #2


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.
 
  • #3


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?
 
  • #4


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
 

1. Why is my MATLAB code for Rutherford Scattering not running?

There could be several reasons why your code is not running. Some common issues include syntax errors, missing input or output files, or incorrect variable names. It's important to carefully check your code for any mistakes and ensure that all necessary files are present.

2. How do I fix a "Matrix dimensions must agree" error in my Rutherford Scattering code?

This error usually occurs when trying to perform an operation on matrices with different dimensions. To fix this, you can use the "size" function to ensure that all matrices have the same number of rows and columns before performing any operations.

3. How can I improve the efficiency of my Rutherford Scattering code?

One way to improve efficiency is to preallocate memory for your matrices and vectors before running your code. This can help reduce the time it takes to run your code by avoiding unnecessary reallocations of memory.

4. Why am I getting incorrect results from my Rutherford Scattering simulation?

There are a few potential reasons why your results may be incorrect. It's important to double check your equations and make sure they are accurate. You should also check your input values and ensure they are correct. Additionally, if you are using random numbers in your simulation, make sure they are being generated in a consistent manner.

5. Can I use a different programming language to simulate Rutherford Scattering?

Yes, Rutherford Scattering simulations can be performed in various programming languages. However, MATLAB is a popular choice due to its built-in functions for matrix operations and data visualization. Other options include Python, C++, and Fortran.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
6
Views
914
  • STEM Educators and Teaching
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top