Why when I assign x(n)=0.5 and x(n-1)=0.055 they are gathered into one vector? as x=[0.5 0.055]

1 view (last 30 days)
I am doing a code for Newton-Raphson using Secant method as well. However, I should guess two values! How can I do that? you can see the attachment for my code. Please help if you can.

Accepted Answer

Walter Roberson
Walter Roberson on 31 Oct 2016
You might as well use rand() for the initial guesses, unless you have physical knowledge that would suggest other values.
You are going to need to change your code. You have
f(x(n))=((-(100-DOT(i)))/(100))+(1/(x(n)-60))*(x(n)*exp(-Time(i)/x1)-60*exp(-Time(i)/60))
for i=1:43
while abs((f(x1)))>0.001
x(n+1)=x(n)-((x(n)-x(n-1)))*(f(x(n))/(f(x(n))-f(x(n-1))))
x0=x1;
x1=x2;
n=n+1;
end
end
which tries to define a formula for f(x(n)) based upon general n and i values. The only way to define a formula is with the Symbolic Toolbox, and symbolic formulas need to be define in terms of variables rather than array elements.
Adjusted code:
x1 = nan; %we do not know what this variable is for or how it should be initialized
x2 = nan; %we do not know what this variable is for or how it should be initialized
for i=1:43
f = @(X, X1) ((-(100-DOT(i)))/(100))+(1/(X-60))*(X*exp(-Time(i)/X1)-60*exp(-Time(i)/60));
while abs((f(x(n), x1)))>0.001
x(n+1) = x(n)-((x(n) - x(n-1))) * (f(x(n), x1) / (f(x(n),x1) - f(x(n-1),x1)));
x0=x1;
x1=x2;
n=n+1;
end
end
This adjusted code is unlikely to work, as I see errors in your formula, but it will at least pass syntax checking.
  2 Comments
Khalil Ishaq
Khalil Ishaq on 31 Oct 2016
Edited: Khalil Ishaq on 31 Oct 2016
I have made a couple of mistakes in my code. However, I edited it based on your suggestion.
X1=0.1 %First intial guess of x(2)
X0=0.15 %Second Initail guess of x(1)
for i=1:43
f = @(X1) ((-(100-DOT(i)))/(100))+(1/(X1-60))*(X1*exp(-Time(i)/X1)-60*exp(-Time(i)/60));
f = @(X0) ((-(100-DOT(i)))/(100))+(1/(X0-60))*(X0*exp(-Time(i)/X0)-60*exp(-Time(i)/60));
while abs((f(X1)))>0.001
X2=X1-((X1-X0))*(f(X1)/(f(X1)-f(X0)))
n=n+1;
X0=X1;
X1=X2;
end
end
It converges to a value of 30, not sure if this is what I suppose to get. I am using for loop because I have to solve for X 43 times; nevertheless, I got only the solution of the final term i=43. Any Ideas on how I can put these in one vector?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!