Matlab

Moderators: None (Apply to moderate this forum)
Number of threads: 1084
Number of posts: 1765

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
unknown error Posted by irwinwck on 19 Dec 2009 at 12:57 PM
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
Report
Re: unknown error Posted by franktruijen on 28 Dec 2009 at 7:07 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.