matlab中如何将x1 x2替换成想x(1)和x(2)
例如:函数f2=((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2;中,要将表达式中的将x1x2替换成想x...
例如:函数f2=((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2;中,要将表达式中的将x1 x2替换成想x(1)和x(2),用subs函数,subs(f2,[x1 x2],[x(1) x(2)]) 之后,显示:
??? Error using ==> mupadmex
Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1366
B = mupadmex('mllib::subsref',A.s,inds{:});
Error in ==> tidu_1 at 17
subs(f2,[x1 x2],[x(1) x(2)])
初学者,求大神解答,不胜感激 展开
??? Error using ==> mupadmex
Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1366
B = mupadmex('mllib::subsref',A.s,inds{:});
Error in ==> tidu_1 at 17
subs(f2,[x1 x2],[x(1) x(2)])
初学者,求大神解答,不胜感激 展开
3个回答
展开全部
% 字符串替换
f2 = '((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2'
f2 = strrep(f2,'x1','x(1)');
f2 = strrep(f2,'x2','x(2)')
% 符号替换
L = [1 2]; i = 1;
y = [3 4];
syms ZH x1 x2
f2 = ((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2
f2 = subs(f2,{'x1','x2'},{'x(1)','x(2)'})
% 函数句柄替换
L = [1 2]; i = 1;
y = [3 4];
f2 = @(x1,x2) ((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2
s = func2str(f2);
s = strrep(s,'x1','x(1)');
s = strrep(s,'x2','x(2)');
f2 = str2func(s)
f2 = '((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2'
f2 = strrep(f2,'x1','x(1)');
f2 = strrep(f2,'x2','x(2)')
% 符号替换
L = [1 2]; i = 1;
y = [3 4];
syms ZH x1 x2
f2 = ((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2
f2 = subs(f2,{'x1','x2'},{'x(1)','x(2)'})
% 函数句柄替换
L = [1 2]; i = 1;
y = [3 4];
f2 = @(x1,x2) ((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2
s = func2str(f2);
s = strrep(s,'x1','x(1)');
s = strrep(s,'x2','x(2)');
f2 = str2func(s)
展开全部
你得有矩阵x【x1,x2】
其中x1 x2 为列向量
x1=x(:,1) x2=x(:,2)
否则你都没有x,哪来的x(1),x(2)
其中x1 x2 为列向量
x1=x(:,1) x2=x(:,2)
否则你都没有x,哪来的x(1),x(2)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
>> f2=sym('((L(i)-ZH)^3/(6*x1*x2)-y(i))*cos((L(i)-ZH)^2/(2*x1*x2))^2')
x1=sym('x(1)');
x2=sym('x(2)');
f2=subs(f2)
f2 =
-cos((ZH - L(i))^2/(2*x1*x2))^2*(y(i) + (ZH - L(i))^3/(6*x1*x2))
f2 =
-cos((ZH - L(i))^2/(2*x(1)*x(2)))^2*(y(i) + (ZH - L(i))^3/(6*x(1)*x(2)))
x1=sym('x(1)');
x2=sym('x(2)');
f2=subs(f2)
f2 =
-cos((ZH - L(i))^2/(2*x1*x2))^2*(y(i) + (ZH - L(i))^3/(6*x1*x2))
f2 =
-cos((ZH - L(i))^2/(2*x(1)*x(2)))^2*(y(i) + (ZH - L(i))^3/(6*x(1)*x(2)))
追问
若是f2是有别的公式求得的,而且比较长,就报错了,这种情况怎么办了?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询