How do I create a function and run it.

5 次查看(过去 30 天)
I don't know anything about MATLAB beyond how to create a new script file and type stuff into it.
I copied the following code from a course handout. So I assume it is correct.
%Program 1.1 Bisection Method
%Computes approximate solution of f(x)=0
%Input: function handle f; a,b such that f(a)*f(b)<0,and tolerance tol
%Output: Approximate solution xc
xc=bisect(f,a,b,tol)
if sign(f(a))*sign(f(b)) >= 0
error('f(a)f(b)<0 not satisfied!') %ceases execution
end
fa=f(a)
fb=f(b)
while (b-a)/2>tol
c=(a+b)/2;
fc=f(c);
if fc == 0 %c is a solution, done
break
end
end
if sign(fc)*sign(fa)<0 %a and c make the new interval
b=c;fb=fc;
else %c and b make the new interval
a=c;fa=fc;
end
xc=(a+b)/2;
It is supposed to solve a single-variable equation by bisection. I understand the bisection logic and the function flow logic seems obvious to me.
I want to give it a function, say x^5+x=1, and have it bracket in on x and get something close to 0.754877666. What do I type into the MATLAB command window to do this?
Question 2. Where do I find answers to simple questions like this? Is there a list of simple examples somewhere? I have gone through the "onramp" stuff and read the MATLAB Primer, which took hours, without learning how to just pass some variables to a pre-defined function.
  1 个评论
Stephen23
Stephen23 2017-2-3
编辑:Stephen23 2017-2-3
"Question 2. Where do I find answers to simple questions like this?"
The MATLAB documentation is the most readable, browsable documentation of any language that I have seen or used. It is organized by topic, has links everywhere to related topics and functions, and gives working examples. The introductory tutorials are a good place to start learning fundamental MATLAB concepts:
Ten minutes practicing using the contents (LHS of the page) will be time well spent: click up a level, see the topics, get used to navigating around!

请先登录,再进行评论。

采纳的回答

Richard Zappulla
Richard Zappulla 2017-2-3
Every function in MATLAB uses the following structure:
function [ouput_vars] = foo(input,vars)
%
% Code goes here....
%
end
In your case, you want the first line to read:
function xc = bisect(funcHandle, a, b, tol)
where funcHandle is a handle for your function. The syntax of a function handle is:
funcHandle = @(variables)(equation)
To use the function, you simply call the variable name as a function, i.e.
f_of_x = funcHandle(pi);
Since your bisection method expects a function of the form f(x) = 0, your function handle would be,
your_fun = @(x)(x^5 + x -1);
Hope this helps!!
  1 个评论
Jay Gourley
Jay Gourley 2017-2-3
Yes, that helps. Thanks for anticipating that I needed more than just the command to type. Your explanation of the breakdown helps a lot.

请先登录,再进行评论。

更多回答(1 个)

John BG
John BG 2017-2-3
function xc=bisect(f,a,b,tol)
if sign(f(a))*sign(f(b)) >= 0
error('f(a)f(b)<0 not satisfied!') %ceases execution
end
fa=f(a)
fb=f(b)
while (b-a)/2>tol
c=(a+b)/2;
fc=f(c);
if fc == 0 %c is a solution, done
break
end
end
if sign(fc)*sign(fa)<0 %a and c make the new interval
b=c;fb=fc;
else %c and b make the new interval
a=c;fa=fc;
end
xc=(a+b)/2;
end
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by