How do I create a function and run it.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jay Gourley
am 3 Feb. 2017
Bearbeitet: Stephen23
am 3 Feb. 2017
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 Kommentar
Stephen23
am 3 Feb. 2017
Bearbeitet: Stephen23
am 3 Feb. 2017
"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!
Akzeptierte Antwort
Richard Zappulla
am 3 Feb. 2017
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!!
Weitere Antworten (1)
John BG
am 3 Feb. 2017
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!