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

  • Thread starter Thread starter danbone87
  • Start date Start date
  • Tags Tags
    Loop Matlab
AI Thread Summary
The discussion revolves around a user seeking help with a MATLAB for loop intended to iterate a variable, specifically updating the variable `ap` in each iteration based on a formula involving `fa` and `fprimea`. The user initially struggles with the implementation, receiving errors related to indexing and function usage. Key points include the need for proper function definitions and understanding MATLAB's indexing system, which starts at 1 rather than 0. The user encounters an error when attempting to access elements incorrectly, mistaking function calls for array indexing. After guidance on syntax and function scope, the user successfully resolves the issue, indicating that they have implemented the necessary changes and achieved their goal.
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
Views
1K
Replies
10
Views
3K
Replies
4
Views
2K
Replies
9
Views
2K
Replies
18
Views
4K
Back
Top