Matlab Indicing (help request)

In summary, the author is trying to model Radioactive Decay using a while loop. He has some trouble with the Euler method and is looking for a way to fix it. He is also looking for a slope formula for radioactive decay.
  • #1
Pythagorean
Gold Member
4,401
313
I'm having a helluva time trying to build the index for this code.

First of all, this is homework, I'll admit, but I've done all I could
to research the problem on my own, so I understand the question I'm trying to ask by now, but I still haven't found an answer

Basically, I'm modeling Radioactive Decay in several different ways on
one plot. The issue I'm having is with the Euler method, because it's a numerical approach, and the last three years of my college life has been studying continuous.

Also, I have to use a while loop.

here is the code, followed by my question:

%----------------------------------------------------------------
%EULER
%----------------------------------------------------------------
i=1
t(1)=0
Ne(1)=100

while t>=ti & t<=tf %<---tf and ti are previously defined
t(i)=delt*(i-1)
Ne(i+1)=(1-lam*delt)*Ne(i)
i=i+1
end
plot(t,Ne,'ro')
------------------------------------------------------------------

Before plot even comes on, I can see that Ne will have one more
value than t does, and when I run it (when I run it, it says the matrices must must have an equal amount of elements and such, and if I scroll
through the run, i can see that Ne has (as I assumed) one more value
than t.

So I try plot(t,Ne(i-1),'ro'), hoping it will return to the whole indix set one period ago for Ne, but no, it just takes the scalar of Ne at the i-1 index and gives me a nice flat line.

Question: How do I cut off the end of the matrices (Ne and t) to the same length like a paper cutter does with paper

OR

How do I enter a final value for one last indice after the loop has created a table (this would go after the 'end' of the loop and before the 'plot'.
 
Physics news on Phys.org
  • #2
to select the the 15 first components of a vector t, type t(1:15).
 
  • #3
I've spent many hours on this code since then. What I actually did was made the line:

t(i)=delt*(i-1)

to say:

t(i+1)=delt*(i)

therefore bumping t up one index (since the first index is already defined as an initial value) which opened up a couple other little problems I had to fix

I'm actually on to Runge-Kutta now. Our teacher gave us an easy three step code for it, but it won't work for me.

I think I have my slope formula wrong. It's supposed to be Radioactive decays first derivative, I thought, but the line doesn't even go in the right direction, so I don't know. I'll be spending all day today trying to figure it out.

i=1
t(1)=ti
Nrk(1)=Not
while Nrk>0 & Nrk<=Not
t(i+1)=ti+delt*i
s=-lam*Nrk(i)*exp(-lam*t(i))
k1=delt*s
k2=(Nrk(i)+k1/2)*delt
Nrk(i+1)=Nrk(i)+k2/2
i=i+1
end
plot(t,Nrk,'b')
clear all
 

1. What is Matlab indexing?

Matlab indexing refers to the process of accessing and manipulating elements in a multidimensional array or matrix using their position or indices. It allows for more efficient and precise data manipulation and analysis.

2. How do I access specific elements in a matrix using indexing?

To access a specific element in a matrix, you can use the row index followed by the column index in parentheses, for example, A(2,3) would access the element in the second row and third column of matrix A.

3. Can I use logical indexing in Matlab?

Yes, Matlab supports logical indexing which allows you to use logical expressions to select elements in a matrix. For example, A(A>5) would return all elements in matrix A that are greater than 5.

4. How do I assign values to specific elements using indexing?

To assign a value to a specific element in a matrix, you can use the same indexing notation as accessing elements but with an equal sign. For example, A(2,3) = 10 would assign the value 10 to the element in the second row and third column of matrix A.

5. Can I use indexing to perform operations on specific rows or columns in a matrix?

Yes, you can use indexing to perform operations on specific rows or columns in a matrix. For example, A(2,:) refers to all elements in the second row of matrix A, and A(:,3) refers to all elements in the third column of matrix A. You can then perform operations on these subsets of data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
936
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top