How can I write the following Algorithm in MATLAB? What is wrong with my code?
Show older comments
Hello,
We have an algorithm like:
Step 1: Set ε, ρ_0,t_1. Set i=1
Step2: If a≤ε, stop.
Step 3: Calculate ∆_i. Set t_{i}^{+}=t_i+∆_i
Step 4: Calculate ρ_i.
Step 5: If b=0, go to Step 6. Otherwise, set t_{i+1}=t_{i}^{+} and go to Step 7.
Step 6: If ρ_i ≥ρ_0, set t_{i+1}=t_{i}^{+}, do something go to Step 8. Otherwise do another thing and go to Step 3 (inner cycle).
Step 7: If ρ_i ≥ρ_0 do something and go to Step 8. Otherwise do another thing and go to Step 8.
Step 8: Set i=i+1 and go to Step 2. (outer cycle)
I have written a code like this:
Set ε, ρ_0,t_1, maxiter. Calculate a, Set i=1
While (a> ε & i<maxiter)
Calculate ∆_i
Set t_{i}^{+}=t_i+∆_i
Calculate ρ_i.
Calculate b.
If b=0
If ρ_i ≥ρ_0
t_{i+1}=t_{i}^{+}
i=i+1
else
while (ρ_i <ρ_0 & b=0)
Calculate ∆_i
Set t_{i}^{+}=t_i+∆_i
Calculate ρ_i.
Calculate b.
end (out of inner cycle)
If ρ_i ≥ρ_0
t_{i+1}=t_{i}^{+}
i=i+1
do something
else
t_{i+1}=t_{i}^{+}
i=i+1
do another thing
end
end
else (beginning if)
t_{i+1}=t_{i}^{+}
If ρ_i ≥ρ_0
do something
i=i+1
else
do another thing
i=i+1
end
calculate a
end (out of outer cycle)
The code is working but it is not working as it should work, I could not find my mistake in the code. Do you have any suggestions for the code? Especially for the loop (inner cycle) between Step 3 and Step 6.
3 Comments
Geoff Hayes
on 3 Jul 2014
Anna - what do you mean by the code is working but it is not working as it should work? Is the output unexpected given the input? Is an error thrown?
It might be more helpful if you could attach the code to your question so that it could be compared (more easily) to the algorithm. While the above pseudo-code is helpful, it is not formatted cleanly (and is incomplete) making it difficult to determine where it might deviate from the algo.
Ilham Hardy
on 3 Jul 2014
To be working and not to be working.. :D
Could you be more specific to describe what you see and what you got?
It is also important to put error messages (if there any) to help us help you.
Regards, IH
(PS. no pun intended)
Answers (0)
Categories
Find more on Neuroimaging in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!