0
$\begingroup$

I am encountering some error while trying to find roots of equation using Bisection method. In this method, you take an interval (a1,b1), here I have taken (0,1). If f(a1)f(b1)<0, then you take p1 =average of a1,b1. And depending on its sign, replace one of a1 or b1 by p1 to get a2,b2 and then you repeat, getting closer to the value of root of the equation. Aim of the code is to get as many values of ai,bi,pi as we want for required approximation.

f[x_] := Log[1 + x] - Cos[x]
a[1] = 0;
Sign[f[a[1]]]
b[1] = 1;
Sign[f[b[1]]]
p[i] = -1;
For[i = 1, i <= 4, i++, 
 If[f[a[i]]*f[b[i]] < 0, Print[a[i], " and ", b[i]]
    p[i] = (a[i] + b[i])/2
    If[Sign[f[p[i]]] == Sign[f[a[i]]], a[i + 1] = p[i]
        b[i + 1] = b[i], b[i + 1] = p[i] 
        a[i + 1] = a[i]], 
  Print["Completed ", i, "iterations. Last iteration:No root found"]]]

Somehow the code is not executing. I am getting Set::write: Tag Times in 1/2 Null If[Sign[-Cos[<<1>>]+Log[Plus[<<2>>]]]==-1,a[i+1]=p[i] b[Plus[<<2>>]]=b[i],b[i+1]=p[i] a[Plus[<<2>>]]=a[i]] is Protected. >>

Also, in 8th line, if I add p[i] along with a[i],and b[i] in the print statement, the error gets worse. So I cant even print pi inside the loop.

Please help. I am an amateur and it most probably is just a basic syntax mistake, but I am new to mathematica and I've spent 2 hours on this with no avail. Thanks!

$\endgroup$

closed as off-topic by MarcoB, m_goldberg, Henrik Schumacher, rhermans, Carl Lange Aug 2 at 22:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, m_goldberg, Henrik Schumacher, rhermans, Carl Lange
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ Use ;s more liberally. $\endgroup$ – user6014 Jul 30 at 21:06
  • 1
    $\begingroup$ Whoops, I am ashamed. I tried adding ;s again and it worked :) $\endgroup$ – Nemank Yadav Jul 30 at 21:10
  • 3
    $\begingroup$ Gonna throw out the obligatory don't use for loops link. $\endgroup$ – b3m2a1 Jul 30 at 21:32
  • $\begingroup$ It seems that a comment solved your problem and at the end it was just a simple mistake. That implies that your question may be put on-hold. Small mistakes are off-topic in the sense we don't want to archive them forever, because they are unlikely to help any future visitors. You should not take offence. It's ok to ask, don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. $\endgroup$ – rhermans Aug 2 at 9:19
0
$\begingroup$

If you look at the code in Mathematica 12, you'll see the faint xs (multiplication sign, not an 'ex') at the end of some lines. These are actually hints to the user that there might be an unexpected multiplication happening in your code. To fix this, you'll want to add semi-colons after the individual lines where the x's are.

enter image description here

I am not sure if this is "correct" in terms of the results, but it no longer emits errors:

f[x_] := Log[1 + x] - Cos[x]
a[1] = 0;
Sign[f[a[1]]]
b[1] = 1;
Sign[f[b[1]]]
p[i] = -1;
For[i = 1, i <= 4, i++, 
 If[f[a[i]]*f[b[i]] < 0, Print[a[i], " and ", b[i]];
    p[i] = (a[i] + b[i])/2;
    If[Sign[f[p[i]]] == Sign[f[a[i]]], a[i + 1] = p[i];
        b[i + 1] = b[i], b[i + 1] = p[i] ;
        a[i + 1] = a[i]], 
  Print["Completed ", i, "iterations. Last iteration:No root found"]]]

There are most certainly errors elsewhere in your code that you'll need to work out to get the correct answer for your numeric algorithm, however.

$\endgroup$
  • $\begingroup$ Thanks a Lot! I didnt know I was making such a basic mistake. Its working! $\endgroup$ – Nemank Yadav Jul 30 at 21:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.