绘图日志(n超过k) [英] Plot log(n over k)

查看:103
本文介绍了绘图日志(n超过k)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过Matlab,而且我真的不知道如何修复代码.我需要用k从1到1000绘制log(1000在k上).

I've never used Matlab before and I really don't know how to fix the code. I need to plot log(1000 over k) with k going from 1 to 1000.

y = @(x) log(nchoosek(1000,x));

fplot(y,[1 1000]);

错误:

Warning: Function behaves unexpectedly on array inputs. To improve performance, properly
vectorize your function to return an output with the same size and shape as the input
arguments. 
In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 241)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 196)
In fplot>vectorizeFplot (line 196)
In fplot (line 166)
In P1 (line 5)

推荐答案

代码有几个问题:

  • nchoosek 不会在第二个输入上矢量化,也就是说,它不接受数组作为输入. fplot对于矢量化功能工作更快.否则可以使用,但是会发出警告.
  • 对于第一个输入的如此大的值,nchoosek的结果几乎溢出.例如,nchoosek(1000,500)给出2.702882409454366e+299,并发出警告.
  • nchoosek需要整数输入. fplot通常使用指定范围内的非整数值,因此nchoosek会发出错误.
  • nchoosek does not vectorize on the second input, that is, it does not accept an array as input. fplot works faster for vectorized functions. Otherwise it can be used, but it issues a warning.
  • The result of nchoosek is close to overflowing for such large values of the first input. For example, nchoosek(1000,500) gives 2.702882409454366e+299, and issues a warning.
  • nchoosek expects integer inputs. fplot uses in general non-integer values within the specified limits, and so nchoosek issues an error.

您可以利用阶乘与伽玛函数和Matlab具有 gammaln 这一事实,它可以直接计算对数函数的功能:

You can solve these three issues exploiting the relationship between the factorial and the gamma function and the fact that Matlab has gammaln, which directly computes the logarithm of the gamma function:

n = 1000;
y = @(x) gammaln(n+1)-gammaln(x+1)-gammaln(n-x+1);
fplot(y,[1 1000]);

请注意,您得到的图具有指定范围内所有 x y 值,但实际上二项式系数仅针对非负整数定义.

Note that you get a plot with y values for all x in the specified range, but actually the binomial coefficient is only defined for non-negative integers.

这篇关于绘图日志(n超过k)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆