注册 登录
编程论坛 Matlab

字符变量可以进行逻辑运算吗?

jiayongwang 发布于 2006-07-05 09:13, 2156 次点击
我在程序中运用字符变量来解决问题,不知道能进行"逻辑非"运算吗
程序如下
:

function g=f(x)
n=length(x);
if n<3
disp('n<3');
return;
else
if n==3
g=x(1)*x(2)*x(3);
elseif n==4
g=(x(1))^(2)*(x(2))^(2)*(x(3))^(2)*(x(4))^(2)+(x(1))^(2)*x(2)*(x(3))^(2)*x(4)+x(1)*(x(2))^(2)*x(3)*(x(4))^(2);%用-1次幂来表示逆元

else
for i=2:n-2
y1(i)=x(i); %用来表示f(n-1的新变量
y2(i)=x(i);
end

y1(1)=(x(1))^(2);
y1(n-1)=(x(n-1))^(2);
y2(1)=(x(1))^(2)*x(n-1)+x(1)*(x(n-1))^(2);
g=f(y1)*x(n)+f(y2)*(x(n))^(2);
g=expand(g); %zhangkai
pretty(g) %xingshi hua
end
end

其中,我用(Xi)^2来代替(Xi)的非
6 回复
#2
zhangenter2006-07-05 10:14

不能
>>a=0
a=

0
>>~a
ans=

1
>>a=sym('0')
a=

0
>>~a
???function 'not' is not defined for value of class 'sym'

Error in ==>
not at 12
builtin('not',varagin{:});

#3
jiayongwang2006-07-05 10:27
哦,谢谢了
#4
zhangenter2006-07-05 10:46
g=expand(g)和pretty(g)都是对字符类型处理,你这个程序问题很大啊   
#5
jiayongwang2006-07-05 18:25

g=expand(g)和pretty(g)都是对符号表达式的化简,我的程序能得到预想的结果,只不过体现不了"逻辑非"
下面是运行的结果:

x=sym('[x1,x2,x3,x4]')

x =

[ x1, x2, x3, x4]

>> x(5)=sym('x5')

x =

[ x1, x2, x3, x4, x5]

>> f(x)

4 2 2 4 4 2 2 2 2 4
x5 x1 x2 x3 x4 + x5 x1 x2 x3 x4 + x5 x1 x2 x3 x4

2 2 2 2
+ x2 x3 x5 x1 x4 + x2 x3 x5 x1 x4

ans =

x5*x1^4*x2^2*x3^2*x4^4+x5*x1^4*x2*x3^2*x4^2+x5*x1^2*x2^2*x3*x4^4+x2*x3*x5^2*x1^2*x4+x2*x3*x5^2*x1*x4^2
望版主指点迷津.

#6
Matlab2006-07-07 15:18
好强,以后要多来看看,小弟是新来的,对MATLAB还不熟悉呢
#7
abingchem2006-07-07 16:02

欢迎欢迎

1