欢迎光临散文网 会员登陆 & 注册

Matlab--函数拟合、Dijkstra算法

2022-12-29 19:59 作者:洛溪い  | 我要投稿

函数拟合

题目:

题目1

代码:

>> x=[0,34,67,101,135,202,259,336,404,471];

>> y=[15.18,21.36,25.72,32.29,34.03,39.45,43.18,43.46,40.83,30.75];

function [fitresult, gof] = createFit(x, y)

[xData, yData] = prepareCurveData( x, y );

ft = fittype( 'gauss1' );

opts = fitoptions( 'Method', 'NonlinearLeastSquares' );

opts.Display = 'Off';

opts.Lower = [-Inf -Inf 0];

opts.StartPoint = [43.46 336 210.279614505574];

[fitresult, gof] = fit( xData, yData, ft, opts );

figure( 'Name', '无标题拟合 1' );

h = plot( fitresult, xData, yData );

legend( h, 'y vs. x', '无标题拟合 1', 'Location', 'NorthEast', 'Interpreter', 'none' );

xlabel( 'x', 'Interpreter', 'none' );

ylabel( 'y', 'Interpreter', 'none' );

grid on

Dijkstra算法:

题目:

题目2

代码:

clc,clear all

a=zeros(8);

a(1,2)=4;a(1,3)=6;

a(2,4)=5;a(2,5)=4;

a(3,4)=4;a(3,5)=7;

a(4,6)=9;a(4,7)=7;

a(5,6)=5;a(5,7)=6;

a(6,8)=4;a(6,7)=5;

a(7,8)=1;

a=a+a'

a(find(a==0))=inf

pb(1:length(a))=0;pb(1)=1;

index1=1; 

index2=ones(1,length(a));

d(1:length(a))=inf;d(1)=0;

temp=1;

while sum(pb)<length(a)

tb=find(pb==0);

d(tb)=min(d(tb),d(temp)+a(temp,tb));

tmpb=find(d(tb)==min(d(tb)));

temp=tb(tmpb(1));

pb(temp)=1;

index1=[index1,temp];

temp2=find(d(index1)==d(temp)-a(temp,index1));

index2(temp)=index1(temp2(1));

end

d,index1,index2

Matlab--函数拟合、Dijkstra算法的评论 (共 条)

分享到微博请遵守国家法律