Output of function not variable I assign

When I run this function I want that vector to be called u so I can use it in another script file. Instead it calls it "ans" which I can't use. What is going on? Here is the function, just run it for n=4.
function [u]=finitevolume_KG(n)
%n is the number of volumes
delta_x = 1/n;
A=zeros(n-1);
A(1,1)=1;
A(1,2)=-1;
for i=2:n-2
A(i,i-1)=-1;
A(i,i)=2;
A(i,i+1)=-1;
end
for i=n-1
A(i,i-1)=-1;
A(i,i)=2;
end
f=ones(n-1,1);
f_x=((delta_x)^2)*f;
u = (A)\(f_x);

답변 (2개)

per isakson
per isakson 2013년 3월 17일
편집: per isakson 2013년 3월 18일

1 개 추천

Try the call
u = finitevolume_KG( 4 )
and make it a habit to end your functions with the keyword "end".

댓글 수: 2

Jan
Jan 2013년 3월 18일
I still omit the trailing "end" to support the small part of Matlab 6.5 users.
per isakson
per isakson 2013년 3월 19일
Yes, but "end" kind of indicates the end of the workspace of the function.

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

Jan
Jan 2013년 3월 18일

0 개 추천

Some simplifications:
function u = finitevolume_KG(n)
%n is the number of volumes
delta_x = 1/n;
A = zeros(n-1, n-1);
A(1,1) = 1;
A(1,2) = -1;
A(2:n-2, 1:n-3) = -1;
A(2:n-2, 2:n-2) = 2;
A(2:n-2, 3:n-1) = -1;
A(n-1, n-2) = -1;
A(n-1, n-1) = 2;
u = A \ (delta_x ^ 2 * ones(n-1, 1));

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2013년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by