
首先建立以个函数文件,代码如下面两行,保存到工作空间,文件名一定要是fun(因为下面的函数文件我命名为fun了)
function Vt=fun(a,t)
Vt=10-(10-a(1))*exp(-t/a(2)) %a(1)=V0,a(2) =τ
接着是主程序,包括下面全部:
%下面是数据
t=[0.5 1 2 3 4 5 7 9]
Vt=[6.36 6.48 7.26 8.22 8.66 8.99 9.43 9.63]
%matlab拟合的初值选择是一个麻烦的山轮问题
a0=[6 0.5 ]%这是V0,τ的初值,搜脊得根据你的问题选取,我这里也是随便取的,
A=lsqcurvefit(@fun,a0,t,Vt)
%下面你可以画一个图形做拟合对逗漏信比
f=fun(A,t)
plot(t,f,'r',t,Vt,'*-b')
legend('拟合','实际数据',2)
结果:
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
A =
5.55767167903105 3.50020448743878
第一题:A = pascal(7) %生成7*7 pascal矩阵
B = A([1,3,5],[5,6,7]) %构成矩阵B
C = A(5:7,1:3)%构成矩阵C
D = B-1*C
A =
1 1 1 1 1 1 1
1 2 3 4 5 6 7
1 3 610152128
1 41020355684
1 5153570 126 210
1 62156 126 252 462
1 72884 210 462 924
B =
1 1 1
152128
70 126 210
C =
1 515
1 621
1 728
D =
0-4 -14
1415 7
69 119 182
第二题:
function A=prime_number()
N=100
j=1
for i=2:N
if prod(rem(i,2:(i-1)))~=0
A(j)=i
j=j+1
end
end
A =
2 3 5 7111317192329313741434753596167717379838997
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)