Could you point out any errors in my code?

for i = 1:43
f(2)=(1/(x(2)-60))*((x(2)*exp(-t(i)/x(2)))-(60*exp(-t(i)/60)))-((100-DOT(i))/100);
f(1)=(1/(x(1)-60))*((x(1)*exp(-t(i)/x(1)))-(60*exp(-t(i)/60)))-((100-DOT(i))/100);
x(1)=0.1;
x(2)=0.11;
while 1
f(x)= (1/(x-60))*((x*exp(-t(i)/x))-(60*exp(-t(i)/60)))-((100-DOT(i))/100);
x(3)= x(2) - (f(x(2)))*((x(2) - x(1)/(f(x(2)) - f(x(1)))));
f(3)= (1/(x(3)-60))*((x(3)*exp(-t(i)/x(3)))-(60*exp(-t(i)/60)))-((100-DOT(i))/100);
if abs(f(3))>0.0001
x(3)=x(2);
x(2)=x(1);
else break
end
end
end
*EDIT:
I made a few edits to this code. It's as below. Don't worry about the variable values except x, as I already have them and they are 43x1 matrices.
The update code is:*
for i = 1:43
x1=0.1;
x2=0.11;
f2=(1/(x2-60))*((x2.*exp(-t(i)./x(2)))-(60.*exp(-t(i)./60)))-((100-DOT(i))./100);
f1=(1/(x1-60))*((x1.*exp(-t(i)./x(1)))-(60.*exp(-t(i)./60)))-((100-DOT(i))./100);
while 1
f2=(1/(x2-60))*((x2.*exp(-t(i)./x(2)))-(60.*exp(-t(i)./60)))-((100-DOT(i))./100);
f1=(1/(x1-60))*((x1.*exp(-t(i)./x(1)))-(60.*exp(-t(i)./60)))-((100-DOT(i))./100);
x3= x2 - (f(2))*((x2 - x1)/(f(2) - f(1)));
f3= (1/(x3-60))*((x3.*exp(-t(i)./x3))-(60*exp(-t(i)./60)))-((100-DOT(i))./100);
if abs(f3)>0.0001
x3=x2;
x2=x1;
else break
end
end
end
This code works properly but doesn't give the answer I need.

Answers (2)

You don’t tell us what error that line threw, so I’m left with two possibilities:
  1. You may need to vectorise it. (See Array vs. Matrix Operations for details.) You haven’t told us the dimensions of ‘t’ or ‘DOT’, so I assume here they are both equal-sized vectors.
  2. It produces a row vector (assuming ‘t’ is a row vector), and you did not allocate a second dimension for it.
See if using this edited version produces the correct result:
f(2,:)=(1/(x(2)-60))*((x(2)*exp(-t/x(2)))-(60*exp(-t/60)))-((100-DOT)/100);
You will encounter other problems, because you are referencing ‘x’ as a subscript in your ‘f(x)’ assignment, and they are not positive integers. Assuming (again) you want that to be a function, it becomes this anonymous function:
fx = @(x) (1/(x-60))*((x*exp(-t/x))-(60*exp(-t/60)))-((100-DOT)/100);
and the subsequent ‘f(3)’ assignment then becomes:
f(3,:) = fx(x(3));
That should solve some of the problems with your code, but since I don’t know what you want to do, I can’t be more specific.

5 Comments

I've edited the question.
‘This code works properly but doesn't give the answer I need.’
Seriously, how do you expect us to somehow know what answer you ‘need’, and then change your code to provide it?
Well I had seen clues that Walter was working on a beta version of the Mind Reading Toolbox, but last May I think something went horribly wrong and he swirled into a cosmic vortex and vanished, with only extremely rare blips from him when the space time fabric ripples in just right way.
He can’t finish it soon enough!
How do we best offer him support and encouragement?
Maybe try his personal web page? https: // plus. google. com / 101655028918118521762 / posts

Sign in to comment.

Well, for some... x, t, f, and DOT are not defined. And your while loop does not have a failsafe to break out which means an infinite loop is possible. And you're defining f(x) so f must be an array but you're passing in fractional indexes for f because x has fractional components. Not only that, but x is a 2 or 3 element array of fractional numbers. Plus when you're dividing things by x you need to use ./ instead of / if you want to divide by all the elements of x. Same for . Use "." instead of "*" if you want the multiplication to apply to all elements and not do a matrix multiply instead.
Finally the lack of comments is a major failing of this chunk of code.
Please see this link

2 Comments

I've edited the question.
Why make it difficult for us to help you by saying "Don't worry about the variable values except x, as I already have them and they are 43x1 matrices."? Why not just include them so we can run your code. Why make us write code to try to come up with something that may or may not work?

Sign in to comment.

Categories

Asked:

on 9 Mar 2015

Commented:

on 9 Mar 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!