大家好,我有一个代码已经完成,我做了我想做的一切我只是坚持显示我的输出。我没有那么多的功能经验,我目前仍然坚持如何绘制我的信息。我写的代码是:
function x = Tridiag(e,f,g,r)
% Tridiag: Tridiagonal equation solver banded system
% x = Tridiag(e,f,g,r): Tridiagonal system solver.
clc;clear;
% input:
D = 2;
U =1;
L = 0.2;
c0 = 80;
c10 = 10;
dx = 1;
n=9;
e = ones(n,1)*(-2.5);
f = ones(n,1)*(4.2);
g = ones(n,1)*(-1.5);
r = [200;0;0;0;0;0;0;0;0;15];
% output:
% x = solution vector
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
x(n) = r(n)/f(n);
for k = n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end
end
目前运行该功能时,它会将输出显示为ans = 68.641,58.87 ....等等。我想以下列方式绘制我的信息:
x = 0.1:1:10
plot(x,ans)
我遇到x = 0.1:1:10的问题,覆盖了我的函数的值,matlab不理解" ans"。我尝试使用像c = Tridiag(e,f,g,r); plot (x,c)
这样的变量来制作ans,但这不起作用。任何帮助都会受到很大的限制
答案 0 :(得分:0)
有一些问题和一些不同的解决方案:
insert into @toErase select 1, 1, 5
insert into @toErase select 1, 3, 36
和ans
的长度是不一样的。我将假设x
的长度应为x
ans
。你应该按照自己的意愿进行扩展。
您可以在脚本中运行所有内容,而不是函数。
n=9
或指定一个功能:
clc; clear;
D = 2;
U =1;
L = 0.2;
c0 = 80;
c10 = 10;
dx = 1;
n=9;
e = ones(n,1)*(-2.5);
f = ones(n,1)*(4.2);
g = ones(n,1)*(-1.5);
r = [200;0;0;0;0;0;0;0;0;15];
% output:
% x = solution vector
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
y(n) = r(n)/f(n);
for k = n-1:-1:1
y(k) = (r(k)-g(k)*y(k+1))/f(k);
end
x = 1:1:n;
plot(x,y)
并称之为:
function x = Tridiag(e,f,g,r,n)
% Tridiag: Tridiagonal equation solver banded system
% x = Tridiag(e,f,g,r): Tridiagonal system solver.
% input:
D = 2;
U =1;
L = 0.2;
c0 = 80;
c10 = 10;
dx = 1;
% output:
% x = solution vector
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
x(n) = r(n)/f(n);
for k = n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end
end