# How can I fix my for loop troubles in Matlab?

• MATLAB
• danbone87
In summary, you are trying to run a for loop with a variable ap that will replace ap-1 in each iteration, but you are having difficulty with it. You need to learn how to write functions. Look them up in the help.
danbone87
Hi, I have this for loop where i want it to iterate for a variable ap = ap-1 -f(x)

and i want ap-1 to replace ap in each iteration.

It's pretty simple but i just can't get it to work out. any help would be appreciated and here's my code btw.

clc
a=1
fa=exp(a)*((-a^2)+(2*a)-2)+2.723684615938410;
fprimea=exp(a)*(a^2);
ap=a+(fa/fprimea);
tol=1*(10^-6);
f=abs(ap-a);
n=15;

for k=1:10
ap=a-(fa/fprimea);
f=abs(ap-a)
a=ap
end

From the look of it, you need to learn how to write functions. Look them up in the help.

Right now you're just calculating the fa and fprimea junk once.

- Warren

I've been working with those and i keep getting this dang error "index must be a positive integer or logical."

not sure what that means... any ideas?

It probably means you're trying to use 0 to index a vector or matrix. MATLAB indices begin at 1.

- Warren

sorry, but I am not sure what that means at all :[

Well, I can't say for sure unless you show me the code and indicate what line is causing the error.

- Warren

function [f ap]=NR(fa,fprimea,a,tol,f,iterations)

a=1
fa=exp(a)*((-a^2)+(2*a)-2)+2.723684615938410;
fprimea=exp(a)*(a^2);
ap=a+(fa/fprimea);
tol=1*(10^-6);
f=abs(ap-a);
n=15;
iterations= input('how many interations? ')

for k=1:iterations
ap=a-(fa(a)/fprimea(a)); <----- error is in this line
f=abs(ap-a)
a=ap
end

end

error: ? Attempted to access fa(0.998012); index must be a positive integer or logical.

Error in ==> matlabsss at 14
ap=a-(fa(a)/fprimea(a));

In this line:

ap=a-(fa(a)/fprimea(a));

Are you just trying to multiply fa by a and fprimea by a? If so, you need to use * to represent multiplication. MATLAB is interpreting your use of parentheses as an array lookup, e.g. fa(a) is the ath element of fa.

- Warren

okay, i have a function going and all that and it's still doing the same thing. it's not modifying the a to make it equal to the ap of the last iteration...

a only exists inside your function. It disappears as soon as the function is done running. You're passing ap out.

- Warren

I got it!

thanks so much for the help

## 1. How do I create a for loop in Matlab?

To create a for loop in Matlab, you can use the for keyword followed by the loop variable, a set of values to iterate over, and the code to be executed within the loop. For example, for i = 1:10, disp(i), end will print the values 1 through 10.

## 2. Why is my for loop in Matlab not working?

There could be several reasons why your for loop in Matlab is not working. Some common mistakes include incorrect syntax, not defining the loop variable correctly, or not updating the loop variable within the loop. Make sure to check your code for these errors and troubleshoot accordingly.

## 3. How can I optimize my for loop in Matlab?

One way to optimize your for loop in Matlab is to preallocate memory for your variables before the loop. This can improve performance by avoiding the need to resize the variable with each iteration. Additionally, you can try vectorizing your code, which involves performing operations on entire arrays instead of individual elements.

## 4. How do I exit a for loop in Matlab?

To exit a for loop in Matlab, you can use the break keyword. This will immediately terminate the loop and continue execution of the code after the loop. You can also use the continue keyword to skip to the next iteration of the loop without executing the remaining code within the loop for that iteration.

## 5. How do I debug my for loop in Matlab?

To debug your for loop in Matlab, you can use the dbstop function to set breakpoints within your code. This will pause the execution of the loop at the specified breakpoints, allowing you to inspect the values of variables and troubleshoot any errors. Additionally, you can use the disp function to print out values within the loop to track their progress and identify any issues.

• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
737
• MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
799
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K