Octave contour与surf

绘制等高线:contour

网状图绘制:surf

如下画网状图surf:

%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
fprintf('Visualizing J(theta_0, theta_1) ...\n')

theta0_vals = linspace(-10,10,100);
theta1_vals = linspace(-1,4,100);

J_vals = zeros(length(theta0_vals),length(theta1_vals));
m = length(X);
for i = 1:length(theta0_vals)
  for j = 1:length(theta1_vals)
    t = [theta0_vals(i);theta1_vals(j)];
    J_vals(i,j) = 1/(2*m)*sum((X*theta - Y).^2);
  end
end

figure;#指定不同的图

% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';

surf(theta0_vals,theta1_vals,J_vals); 

如下画等高线图contour:

figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20));#J_vals是转置过后的

xlabel('theta_0'); ylabel('theta_1');
hold on;

plot(theta(1),theta(2),'rx','MarkerSize', 10, 'LineWidth', 2);

网状图与等高线同时绘制

在某个情境下,可出现如下运行结果:


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值