필터 지우기
필터 지우기

running m file from command window

조회 수: 3 (최근 30일)
Rao
Rao 2012년 8월 5일
i have this code for bisection method to calculate smallest root
% if true
function[x,e]=MyBisect(f,a,b,n)
%Does n iterations of the bisection method for a function f.
%Inputs f--Inline function
% a,b--Left and Right edges of interval
% n-- no.of bisections to do
%Outputs x--estimated solution of f(x)=0
% e--An upper bound on error
format long
c=f(a);d=f(b);
if c*d>0.0
error('Function has same signs')
end
disp(' x y')
for i=1:n
x=(a+b)/2;
y=f(x);
disp([ x y])
if y==0.0
e=0;
break
end
if c*y<0
b=x;
else
a=x;
end
e=(b-a/2); % code
endcode
end
i dont know how to run it from command window
f is equation of which to find the root eg(x-3-cosx)
a and b value is 1 and 2 resp
n no of iterations

답변 (1개)

Wayne King
Wayne King 2012년 8월 5일
편집: Wayne King 2012년 8월 5일
Just save your function MyBisect.m in a folder that either already is on the Matlab path, or save it in a folder, and then add that folder to the Matlab path with
>>addpath 'path/to/folder'
or use pathtool
For example, assume that you are using Windows and that you create a folder called mfiles. Save MyBisect.m in that folder and then enter
>>addpath 'c:\mfiles'
After that command executes, you should be able to enter
>>which MyBisect
and see that Matlab recognizes the path to the Matlab program.
Now you should be able to call it from the command line with the syntax:
[x,e]=MyBisect(f,a,b,n)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by