Matlab问题,高手请教!!

作者&投稿:高广 (若有异议请与网页底部的电邮联系)
一个matlab小问题,请教高手!~

我告诉你吧,很简单的。幂指数的MATLAB表达式是exp(x),所以你的"e^(4)"应该写成exp(4),为什么是exp呢,因为幂的英文单词是exponentiation
加油!MATLAB是第四代高级语言~我很喜欢MATLAB

我帮你改了下
function [Y]=poly2str(X)
if isvector(X)==0,
disp('输入错误:输入X不是一个向量,请输入代表一个多项式的向量!');
return;
end;
Yt=[];%临时
n=length(X);
for i=1:n,
%+
if(i~=1&&X(i)>0)
Yt=[Yt '+'];
end;
% 系数
if X(i)==0,

elseif X(i)==1&&i~=n,

else
Yt=[Yt num2str(X(i))];
end;
% 幂数
if (i==n-1),
Yt=[Yt 'x'];
elseif(i==n),

else
Yt=[Yt strcat('x^',num2str(n-i))];
end;
end;
Y=Yt;

end

你试试
其实你最大的错误是用了continue
其次输出参数要用方括号括起来,最后连接字符串用strcat函数。其他的没什么问题。
PS:一楼的朋友,恕我直言,不要轻易的否定一个人的做法,这个函数大体上没有错误的,只有一些小纰漏。还有我的版本就没有poly2str函数。

function [R, ind] = randinterval(N, Interval, Weight)
% RANDINTERVAL - random numbers within multiple intervals
%
% R = RANDINTERVAL(N, INTERVALS) returns a N-by-N matrix R of random numbers
% taken from a uniform distribution over multiple intervals.
%
% Similar to RAND, RANDINTERVAL([N M],..) returns a N-by-M matrix, and
% RANDINTERVAL([N M P ...],..) returns a N-by-M-by-P-by-.. array. Note
% that a notation similar to rand(N,M,P,..) is not available.
%
% INTERVALS is a N-by-2 matrix in which the rows specify the intervals from
% which the numbers are drawn. Each interval is given by a lower bound
% (in the first column of INTERVALS) and an upper bound (2nd column of
% INTERVALS). If the lower bound equals the upper bound, no numbers will
% be drawn from that interval. The upper bound cannot be smaller than the
% lower bound.
% If INTERVALS is a vector (K-by-1 or 1-by-K), the intervals are
% formed as (INTERVALS(1),INTERVALS(2)), (INTERVALS(2),INTERVALS(3)),
% ... , (INTERVALS(K-1),INTERVALS(K)). In this case, the number of
% intervals N is one less than the number of elements K, i.e., N=K-1.
% The relative length of an interval determines the likelihood that a
% number is drawn from that interval. For instance, when INTERVALS is [1
% 2 ; 10 12], the probablity that a number is drawn from the first
% interval (1,2) is 1 out of 3, vs 2 out of 3 for the second interval (10,12).
%
% R = RANDINTERVAL(N, INTERVALS, WEIGHTS) allow for weighting each
% interval specifically. WEIGHTS is vector with N numbers. These numbers
% specify, in combination with the length of the corresponding interval,
% the relative likelihood for each interval (i.e., the weight). Larger
% values of W(k) increases the likelihood that a number is drawn from the
% k-th interval.
% For instance, when INTERVALS is [11 12 ; 20 21] and WEIGHTS is [3 1], the
% probability that a numbers is drawn from the first interval is 3 out of
% 4, vs 1 out of 4 for the second interval. Note that both intervals have
% the same length. When the intervals do not have the same lengths, the
% likelihood that a number is drawn from an interval k, with length L(k)
% and weight W(k) is given by the formula:
% p(k) = (W(k) x L(k)) / sum(W(i) ./ L(i)),i=1:N
%
% [R, IND] = RANDINTERVAL(..) also returns a index array IND, which has the
% same size as R. IND contains numbers between 1 and the number of
% intervals. Intervals with zero length and/or a weight of zero will not
% be present in IND.
%
% Notes:
% - overlapping intervals are allowed, but may produce hard-to-debug
% results. In this case a warning is issued. On the other, such
% overlapping intervals could be (ab-)used to draw from specific
% distributions.
% - for a single interval (A B) this function is equivalent to
% A + (B-A) * rand(N)
%
% Examples:
% % Basic use, no weights
% R = randinterval([1,10000], [1 3 ; 5 7 ; 10 15 ; 20 26]) ;
% % returns a row vector with 10000 numbers between 1 and 3, between
% % 5 and 7, between 10 and 15, or between 20 and 26. Approximately
% % 2/5 of the numbers is between 20 and 26:
% N = histc(R,[-Inf 1 3 5 7 10 15 20 26 Inf]), N = N ./ sum(N), bar(N)
%
% % Weights
% [R,n] = randinterval([10,100], [1 2 ; 100 200 ; -2 -1 ; 12 12], [1 0 2 3]) ;
% % returns a 10-by-100 matrix with values in the intervals (-2,-1)
% % or (1,2). Roughly 666 numbers are negative:
% sum(R(:)<0), sum(n(:)==3)
% % The interval (100,200) has a weight of zero, and the interval
% % (12,12) has zero length; these are "ignored".
% histc(n(:),1:4)
%
% % Adjacent intervals with weights:
% [R,n] = randinterval([10000,1], [-2:2],[1 4 2 8]) ;
% bar(-2:2,histc(R,-2:2)/100,'histc') ; ylabel('Percentage')
%
% See also RAND, RANDN, RANDPERM
% RANDSAMPLE (Stats Toolbox)
% RANDP (File Exchange)

% for Matlab R13 and up
% version 1.0 (oct 2008)
% (c) Jos van der Geest
% email: jos@jasen.nl

% History
% Created: oct 2008
% Revisions: -

% Argument checks
error(nargchk(2,3,nargin)) ;

if ndims(Interval) ~= 2 || ~isnumeric(Interval) || numel(Interval)<2,
error([mfilename ':IntervalWrongSize'],...
'INTERVALS should be a N-by-2 (or N-by-1, with N>1) numeric matrix.')
end

if min(size(Interval)) == 1,
% Interval is a vector. Make the bounds column vectors
LowerBound = reshape(Interval(1:end-1),[],1) ;
UpperBound = reshape(Interval(2:end),[],1) ;
elseif size(Interval,2) ~= 2
error([mfilename ':IntervalWrongSize'],...
'INTERVALS should be a N-by-2 (or N-by-1) numeric matrix.')
else
% Get the lower and upper bounds of the intervals
LowerBound = Interval(:,1) ;
UpperBound = Interval(:,2) ;
end

if any(UpperBound < LowerBound),
error([mfilename ':NegativeInterval'],...
'Intervals cannot be smaller than 0') ;
end

if nargin==3 && (~isempty(Weight) || ~isnumeric(Weight)),
if numel(Weight) ~= numel(LowerBound),
error([mfilename ':WeightsWrongSize'],...
'The number of weights should match the number of intervals.') ;
end
if any(Weight < 0)
error([mfilename ':WeightsNegative'],...
'Weights cannot be negative.')
end
else
% default, all intervals are equally likely
Weight = 1 ;
end

tmp = [LowerBound UpperBound] ;
if any(isnan(tmp(:)) | isinf(tmp(:))) || any(isnan(Weight(:)) | isinf(Weight(:))),
error('Intervals or weights cannot contain NaNs or Infs.') ;
end

% It is nice to give a warning when intervals overlap. If they do, the
% lowerbound of an interval is smaller than the upperbound of a previous
% interval, when the intervals are in sorted order
tmp = sortrows(tmp) ;
if any(tmp(2:end,1) < tmp(1:end-1,2)),
warning([mfilename ':OverlappingIntervals'], ...
'Intervals overlap.') ;
end

% The trick is to put all the intervals next to each other. Intervals with
% larger weights should become larger, so they are more likely. The whole
% range spans numbers between 0 and IntervalEdges(end):
IntervalSize = (UpperBound - LowerBound) ;
IntervalEdges = [0 ; cumsum(Weight(:) .* IntervalSize(:))].' ;

if IntervalEdges(end) == 0,
% all intervals had a zero length and/or a zero weight, so there is
% nothing to do!
error([mfilename ':AllZero'],...
'All intervals have a length and/or weight of zero.') ;
end

try
% let RAND catch the errors in the size argument
R = rand(N) ; % random numbers between 0 and 1
catch
% rephrase the error
ERR = lasterror ;
ERR.message = strrep(ERR.message,'rand',[mfilename ' (in a call to RAND) ']) ;
rethrow(ERR) ;
end

if isempty(R),
% nothing to do, e.g. when N contains zero.
ind = [] ;
return
end

% To which interval do these random random numbers belong
[ind,ind] = histc(IntervalEdges(end) * R(:),IntervalEdges) ; %#ok, first output is not used

% now map a new series of random numbers between 0 and 1 to their
% respective interval. We have to create a new series, otherwise not the
% all values in an interval are possible.
R(:) = LowerBound(ind) + IntervalSize(ind) .* rand(numel(R),1) ;

% also return interval numbers in same shape as R
ind = reshape(ind,size(R)) ;

% %#debug plots
% E = linspace(min(Interval(:)), max(Interval(:)),100) ;
% N2 = histc(R(:),E) ;
% figure ;
% subplot(2,1,1) ; bar(E,N2,'histc')
% subplot(2,1,2) ; plot(sort(R(:)),'k.')

%%%%%%%%%%%%%%%%%%%%%%%%%这是该函数,保存后直接调用!
>> [k1,k2]=randinterval([1 10],[1 2;5 6],[3 1])

k1 =

5.6154 1.7919 1.9218 1.7382 5.1763 5.4057 1.9355 1.9169 5.4103 1.8936

k2 =

2 1 1 1 2 2 1 1 2 1

1.心形线是圆的外摆线的一种,它是这样形成的:一个动圆沿一个定圆的圆周外部无滑动的滚动(两个圆的半径相等),动圆的圆周上的一固定点运动的轨迹就是心形线。程序如下:
function [xcar,ycar]=drawcardioid(varargin)
% A demonstration to show the form of the cardioid

error(nargchk(0,3,nargin));
nin = nargin;
if nin==0
cx1=0; cy1=0; % The coordinate of the center point.
r=1; % The radius of the circle.
elseif nin==1
cx1=0; cy1=0;
r=varargin;
elseif nin==2
cx1=0;
cy1= vararin;
r = varargin;
else
cx1=vararin;
cy1=vararin;
r=vararin;
end

if nargout==0
flag=1;
else
flag=0;
end

theta = linspace(0,2*pi,120);
x0 = r*cos(theta);
y0 = r*sin(theta);
x1 = x0+cx1;
y1 = y0+cy1;
cx2 = 2*x0+cx1;
cy2 = 2*y0+cy1;
x3=x0.*cos(theta)-y0.*sin(theta);
y3=x0.*sin(theta)+y0.*cos(theta);
for k=1:120
x2(k,:) = cx2(k)+x0;
y2(k,:) = cy2(k)+y0;
xx(k)=cx2(k)+x3(k);
yy(k)=cy2(k)+y3(k);
end
xcar = xx;
ycar = yy;
if flag
plot(x1,y1);
axis([cx1-3.2*r cx1+3.2*r cy1-3.2*r cy1+3.2*r]);
axis manual;
hold on
daspect([1 1 1]);
set(gcf,'doublebuffer','on');
for k = 1:120
plot(x2(k,:),y2(k,:),'g',xx(k),yy(k),'r*');
pause(0.05);
%plot(x2,y2,'color','w');
end
end

2.求a的平方根:
function y=sq(a)
ep=1.0E-5;
x1=1;
x2=(x1+a/x1)/2;
while(abs(x1-x2)>ep)
x1=x2;
x2=(x1+a/x1)/2;
end
y=x2;

3.多种方法迭代:该方程有一实根:-0.7684531615
方法一:
function y=st1()
ep=1.0E-5;
x1=-2;
x2=(-x1^5+2*x1-1)/(5*x1^2);
while(abs(x1-x2)>ep)
x1=x2;
x2=(-x1^5+2*x1-1)/(5*x1^2);
end
y=x2;
该方法发散

方法二:
function y=st2()
ep=1.0E-5;
x1=-2;
x2=(-5*x1^3+2*x1-1)/x1^4;
while(abs(x1-x2)>ep)
x1=x2;
x2=(-5*x1^3+2*x1-1)/x1^4;
end
y=x2;
该方法不稳定。

方法三:
function y=sq()
ep=1.0E-5;
x1=-1;
tim=-x1^5+2*x1-1;
x2=sign(tim/5)*(abs(tim/5))^(1/3);
while(abs(x1-x2)>ep)
x1=x2;
tim=-x1^5+2*x1-1;
x2=sign(tim/5)*(abs(tim/5))^(1/3);
end
y=x2;
该算法稳定收敛。

由于时间有限,最后一题留给你自己做吧。
另外,团IDC网上有许多产品团购,便宜有口碑

帮助中没有呀 搜索rand其中的相关也没有这个函数

人家那个是自定义的函数,当然你没法运行了
randinterval .m 加载在默认路径下,试一试


matlab软件中出现的乱码,都是数学问题的注释,求高人解答,做题需要。好...
matlab对中文支持不好,建议不要装在中文目录matlab对中文支持不好,建议不要装在中文目录。ATLAB 中默认的字体是 Monospaced (即等宽字体),这是一种非常适合用于显示程序源代码的字体。但Monospaced 是种逻辑字体,它在不同语言和操作系统上映射为不同的物理字体。在中文的 Vista 操作系统下,Monospaced...

ATLAB模糊系统设计图书目录
1.5 绘图应用:介绍了MATLAB的图形绘制功能。第2章 模糊系统的理论基础2.1 模糊系统基础:详细解释模糊集、模糊规则和推理系统,以及非线性逼近和问题探讨。2.2 模糊集合和模糊关系:探讨了经典和模糊集合的区别,以及模糊关系的运算和分析。2.3 模糊扩展原理和特殊类型:如凸模糊集合和模糊数的介绍。...

怎样顺利安装数学mathlab软件?
此时会出现一个编辑选项框,把原来的值删掉,因为这个值里面包含了中文,或许你直接看不到,因为有的时候会用"%USERPROFILE%"的东西来代替掉了.用"C:\/Temp"这个值来代替原来的值,然后再安装matlab7.0,就没有问题了.

matlab中.fig文件转化为.m文件
有个生成matlab代码,在文件下的。

威布尔分布问题解决了吗?
没有,威布尔分布参数的分析法估计较复杂,区间估计值过长,实践中常采用概率纸估计法,从而降低了参数的估计精度.这是威布尔分布目前存在的主要缺点,也限制了它的应用。威布尔分布:在可靠性工程中被广泛应用,尤其适用于机电类产品的磨损累计失效的分布形式。由于它可以利用概率纸很容易地推断出它的...

什么情况下用灰色系统理论来预测?
灰色系统预测在处理数据变化趋势比较单一的数据精度较高,例如随时间大致呈现递增或递减的数据,建立模型之后要检验精度的,精度合格就行。

pixart 原相鼠标什么牌子
鼠标就是其中一个比较好的产品、组偶这个东西的主要几家有:Avago安捷伦(台湾),Pixart原相(台湾),Sunplus凌阳(台湾),EMC义隆(台湾),Atlab艾乐博(韩国),Apexone艾派克森(美国),东贝(台湾),和ST意法半导体(意大利\/法国)。这里面最强的是ST,但安捷伦的应用面是最广的,主要ST太贵。这个和咱买...

米脂县15745943113: Matlab问题, 急求高手解答!!! -
古群吡哌: matlab help:F = getframe(h,rect) specifiesa rectangular area from whi...

米脂县15745943113: MATLAB问题啊,求高手帮助. -
古群吡哌: 存在问题:调用fmincon时提供的初值x0不对.根据目标函数,里面用到了x(6),意味着优化变量数量至少为6;而根据约束条件,里面用到了x(12),这意味着优化变量数量至少为12.综上,x0的长...

米脂县15745943113: 针对matlab的一点问题,请高手解答
古群吡哌: MATLAB 2012中的int函数的版本是1.1.6.5,应该是最新的,我这边和你的警告语句是一样的,数学知识都忘得差不多了,可能是积不出来,然后算数值结果就会有警告.别人的可能版本较低,因此会计算出你给出的那个数值结果,早期版本的...

米脂县15745943113: 请教高手一些MATLAB的问题!!
古群吡哌: 函数的驻点,即一阶导为零的点 多项式求导:polyder 求多项式的根:roots P=[5 0 0 -3 21 -7] %这是多项式系数 A=polyder(P) %求导 x=roots(A) %求根 执行结果 >> P=[5 0 0 -3 21 -7] %这是多项式系数 A=polyder(P) %求导 x=roots(A) %求根 P = 5 0 0 -3 21 -7 A = 25 0 0 -6 21 x = -0.6785 + 0.7408i -0.6785 - 0.7408i 0.6785 + 0.6099i 0.6785 - 0.6099i >> 没有实根~~~~~

米脂县15745943113: 请教高手matlab小波消噪问题.>> load flow; index=1:307; >> s=flow(index); >> subplot(2,2,1); >> subplot(2,2,1); plot(s); ylabel('幅值 A'); >> [c,l]=wavedec(s,3... -
古群吡哌:[答案] 石油主要被用作燃油和汽油,燃料油和汽油组成目前世界上最重要的一次能源之一.石油也是许多化学工业产品如溶剂、化肥、杀虫剂和塑料等的原料.今天88%开采的石油被用作燃料,其它的12%作为化工业的原料.由于石油是一种不可更新原料...

米脂县15745943113: matlab问题请教,matlab高手请进 -
古群吡哌: [z,p,k]=tf2zp(num,den)其中num=1 den={1,32,60} (好像是这样,不行你打 help tf2zp查下说明) 单位脉冲相应就是求他的拉氏反变换应该是-(1/14) E^(-30 t) (-15 + E^(28 t))plot一下即可,(其实显然趋于0)

米脂县15745943113: 数学软件高手:未入门的菜虫请教MATLAB编程问题! -
古群吡哌: 1.心形线是圆的外摆线的一种,它是这样形成的:一个动圆沿一个定圆的圆周外部无滑动的滚动(两个圆的半径相等),动圆的圆周上的一固定点运动的轨迹就是心形线.程序如下:function [xcar,ycar]=drawcardioid(varargin) % A demonstration ...

米脂县15745943113: 求Matlab编程问题,请高手来
古群吡哌: 第一个: clear clc disp('please input n'); n=input('n='); X=zeros(1,n); X(1)=1; X(2)=1; for i=3:n X(i)=X(i-1)+X(i-2); end X 第二个: function y=sign(x) if x<0 y=-1; elseif x>0; y=1 else y=0; 第三个: while(x~=1) if rem(x,2)==0 x=x/2; else x=3*x+1; end ...

米脂县15745943113: matlab 编程初学 遇到问题求高手指点 -
古群吡哌: function test()x=0:0.00001:0.07;for n=1:3 l=0.005+n*0.002; y = 2*( 1./x+1./(x+2*l) ).*cos( -40000/340*x )+2*(1./(x+l)+1./(x+3*l)).*sin(40000...

米脂县15745943113: matlab问题?求高手??? -
古群吡哌: 你看错误提示:“Error using ==> polyfit X and Y vectors must be the same size”使用拟合函数polyfit 时要求向量x和y长度相同,你这里第二行x=0:0.1:0会得到一个空向量x,当然不满足...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网