数值分析MATLAB 修改程序1.2和1.3,分别输出类似于表1.1和1.2的矩阵(即矩阵的第一行是[0 a0 c0 b0 f (c0)])
时间: 2024-05-02 09:18:36 浏览: 39
线性矩阵不等式(LMI)的-MATLAB求解,用matlab求解线性矩阵不等式,matlab源码.zip
5星 · 资源好评率100%
程序1.2修改后的代码:
```matlab
a0 = 1.5;
b0 = 0.8;
c0 = 1.2;
f = @(x) a0 * x.^2 + b0 * x + c0;
h = 0.1;
x = 0:h:1;
y = f(x);
n = length(x);
A = zeros(n);
A(1,1:2) = [1 -1];
for i = 2:n-1
A(i,i-1:i+1) = [1 -2 1];
end
A(n,n-1:n) = [-1 1];
b = zeros(n,1);
b(1) = h^2 * f(x(1));
for i = 2:n-1
b(i) = h^2 * f(x(i));
end
b(n) = h^2 * f(x(n));
y_prime = A\b;
result = [x' y' y_prime'];
disp(' x y(x) y_prime(x)');
disp(result);
```
程序1.3修改后的代码:
```matlab
a0 = 1.5;
b0 = 0.8;
c0 = 1.2;
f = @(x) a0 * x.^2 + b0 * x + c0;
h = 0.1;
x = 0:h:1;
y = f(x);
n = length(x);
A = zeros(n);
A(1,1:3) = [2 -2 0];
A(2,1:4) = [-1 2 -1 0];
for i = 3:n-2
A(i,i-2:i+2) = [0 -1 2 -1 0];
end
A(n-1,n-3:n) = [0 -1 2 -1];
A(n,n-2:n) = [0 -2 2];
b = zeros(n,1);
b(1) = h * f(x(1) + h/2);
for i = 2:n-1
b(i) = h * f(x(i) + h/2);
end
b(n) = h * f(x(n) - h/2);
y_prime = A\b;
result = [x' y' y_prime'];
disp(' x y(x) y_prime(x)');
disp(result);
```
阅读全文