How can I fix my for loop troubles in Matlab?

  • Context: MATLAB 
  • Thread starter Thread starter danbone87
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion centers around troubleshooting a for loop in MATLAB, specifically regarding the iteration of a variable and the handling of function outputs. Participants explore issues related to indexing, function definitions, and variable scope within the context of MATLAB programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in getting a for loop to work correctly, specifically wanting to update a variable in each iteration.
  • Another participant suggests that the original poster needs to learn how to write functions properly, indicating that calculations are being done only once instead of being updated in each iteration.
  • A participant reports encountering an error message related to indexing, specifically mentioning that MATLAB indices start at 1.
  • Further clarification is requested regarding the error, with a suggestion that the issue may stem from trying to use a variable as an index.
  • A participant provides a code snippet where an error occurs, indicating that the function is attempting to access an index that is not valid.
  • Another participant points out that the use of parentheses in the code may be misinterpreted as an array lookup rather than multiplication.
  • The original poster mentions that despite implementing a function, the variable is still not being updated correctly across iterations.
  • A participant explains that the variable 'a' only exists within the function's scope and does not retain its value after the function execution ends.
  • The original poster later expresses gratitude for the assistance received in resolving the issues.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper function usage and the importance of understanding variable scope in MATLAB. However, there are varying levels of understanding regarding the specific error messages and their implications.

Contextual Notes

The discussion highlights limitations in understanding MATLAB's indexing rules and function behavior, particularly concerning variable scope and the need for correct syntax in function definitions.

danbone87
Messages
28
Reaction score
0
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
 
Physics news on Phys.org
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...
 
  • #10
a only exists inside your function. It disappears as soon as the function is done running. You're passing ap out.

- Warren
 
  • #11
I got it!

thanks so much for the help
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K