필터 지우기
필터 지우기

How do I create a function and run it.

조회 수: 3 (최근 30일)
Jay Gourley
Jay Gourley 2017년 2월 3일
편집: Stephen23 2017년 2월 3일
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