unknown error

Hi all,
I'm new to Matlab, this is my first matlab question and i used lots of time to solve it but there are some unknown problem that i cant solve.

Question:
Write a MATLAB program, in a m-file that determines the solution of the equation 8-4.5(x-sin x)=0 by using bisection method. The solution should have approximation eorr of less than 0.001%. Create a table that display the value of X1 , Xu , f(x)f(y) and Ea for each iteration of the bisection process.

My current answer after a week of solving:
a = 2
b = 3
for x=a:b
y(a)=8-4.5*(x-sin(x))
y(b)=8-4.5*(x-sin(x))
c = (a + b)/2
for x=c
y(c)=8-4.5*(x-sin(x))
end
if y(c)==0
answer = y(c), break,
elseif y(a)*y(c)<0
a=c
end
end
----------------------------------------------
I got this error message,
??? Attempted to access y(2.5); index must be a positive integer or logical.

Error in ==> Untitled at 8
y(c)=8-4.5*(x-sin(x))
------------------------------------
Your help and guidance is appreciated. Thanks

Comments

  • Hello

    You made a mistake in your division. Your reply was an c = (a + b)/2.
    The result of this is 2.5, if you want to use this as an matrix index (or array index) it must be an integer. It is possible for you to do a type cast to convert its type.

    Example 1:
    y(int(c))=8-4.5*(x-sin(x))

    int(2.5) = 2

    Example 2:
    round(2.5) = 3

    Or you have to avoid an non-integer result. Negative integers are not allowed.

    Kind regards,
    Frank.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion