Hi!I have to create an m-file for an exercise and I wanted to ask you what is the difference between a function and a script.Which of them is better??

댓글 수: 2

per isakson
per isakson 2013년 12월 5일
the debugger and the profiler works better with functions
evi
evi 2013년 12월 5일
So,do you think that the error message I get has to do with the debugger and the profiler??

댓글을 달려면 로그인하십시오.

답변 (2개)

sixwwwwww
sixwwwwww 2013년 12월 5일

0 개 추천

If you want to re-use some operation again and again then function is better and if you just wanted to start writing codes in MATLAB then script is better at first. Better or not depends upon functionality usually

댓글 수: 8

evi
evi 2013년 12월 5일
Which would you suggest me,for example,for numerical methods,like the implementation of the Jacobi method??
sixwwwwww
sixwwwwww 2013년 12월 5일
I will suggest you using script first
evi
evi 2013년 12월 5일
And is there a difference between these??Because I wrote my code as a function and also as a script and don't get the same result...
sixwwwwww
sixwwwwww 2013년 12월 5일
If you using same parameters in the script and function then you should get the same results. You should take care of values which you input to function and compare those values in the script. If they are same and also implementation in script and function is same then you should get same results beside some minor numerical noise
evi
evi 2013년 12월 5일
When I run the script file for n,that is the maximum number of iterations,I get this message error: ??? Error using ==> mldivide Matrix dimensions must agree
but when I run the function file It gives a result..Why does this happen???
sixwwwwww
sixwwwwww 2013년 12월 5일
can you show me both your script and you function? to find the reason for error
evi
evi 2013년 12월 5일
편집: evi 2013년 12월 5일
The function is like that:
function[]=test(x_0,dimension,e,maxit) H=hilb(dimension); A=H; for j=1:dimension b(j)=sum(A(j,:)); exact(j)=1; end b=b'; exact=exact'; P=diag(diag(A)); F=A-P; x=-P\F*x_0+P\b; end
and the script is like that:
dimension=input('Give dimension: ');
H=hilb(n);
A=H;
e=input('Give e: ');
maxit=input('Give maxit: ');
x_0=input('Give x_0: ');
for j=1:dimension
b(j)=sum(A(j,:));
exact(j)=1;
end
b=b';
exact=exact';
P=diag(diag(A));
F=A-P;
x=-P\F*x_0+P\b;
When I run the function for dimension=50,e: 0.00001,maxit: 50,x_0: zeros(50,1),I get a result,but when I run the script I get this message: ??? Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> ll at 15 x=-P\F*x_0+P\b
Why does this happen???What have I done wrong???
sixwwwwww
sixwwwwww 2013년 12월 5일
I am getting result both from your script and function. Following are the inputs: For function
test(zeros(50, 1), 50, 0.00001, 50)
For script
Give dimension: 50
Give e: 0.00001
Give maxit: 50
Give x_0: zeros(50, 1)
then why you dont get correct result. Can you check what inputs you give to both your function and script?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

evi
2013년 12월 5일

편집:

2013년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by