157行代码实现基于solve用标根法求非周期性不等式实数解的函数

函数的三个输入值依次代表:方程左边、不等号、方程右边

复制代码并保存函数为.m文件并直接调用

思路: 1. 若函数非常值函数,求出函数等于0的实数数组:solvex 2. (1)求出函数值为正/负无穷时的自变量实数数组:epspositivex,epsnegativex (2)保留五位小数为:roundpositivex,roundnegativex (3)将取分式近似值的字符数组转换为数值数组:ratspositivex,ratnegativex (找到不在定义域的拐点) 3. 找出epspositivex/epsnegativex的重复元素,复制两次到solvex(相当于偶次方) 4. 找出epspositivexepsnegative的元素,复制一次到solvex(相当于奇次方) 5. 找出roundpositivex/roundnegativex中一侧函数值非实数,另一侧函数值与不等号同号的元素,复制一次到solvex(找到类对数函数的临界点) 6. 将solvex从小到大排序 7. 引入初值为1的信号t(若令t=-t,可转换t为-1)(从左向右标根前,判断从左侧应从下至上或从上至下) 8. 若不等号为小于,则转换信号t 9. 若solvex最小元素的左侧函数实数值>0或右侧函数实数值<0,则转换信号t 10.solvex的长度>=2时,若t=1则向左闭,若t=-1则向右开 11.solvex的长度=1时,若任意处函数值与不等号同号则解集为R,若任意处函数值与不等号异号则解集为Empty 12.输出

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        
function[set]=fInequality(fleftx,signx,frightx)
syms x x0
fx=fleftx-frightx;
solvex=[];
epspositivex=[];
epsnegativex=[];
if symvar(fx)~=fx
    solvex=solve(fx==0,'real',1);
    epspositivex=solve(limit(fx,x,x0)==1/eps);
    epsnegativex=solve(limit(fx,x,x0)==-1/eps);
end
epspositivex=epspositivex(find(imag(epspositivex)==0));
epsnegativex=epsnegativex(find(imag(epsnegativex)==0));
ratspositivex=rats(double((epspositivex+sign(epspositivex)*eps)));
ratsnegativex=rats(double((epsnegativex+sign(epsnegativex)*eps)));
ratspositivex=str2num(ratspositivex);
ratsnegativex=str2num(ratsnegativex);
t=0;
intersectpositivex=[];
for i=1:length(ratspositivex)
    temppositivex(i)=ratspositivex(i);
    if i>=2
        if temppositivex(i)==temppositivex(i-1)
            t=t+1;
            intersectpositivex(t)=ratspositivex(i);
        end
    end
end
if isempty(intersectpositivex)==0
    for i=1:length(intersectpositivex)
        solvex(length(solvex)+1)=intersectpositivex(i);
        solvex(length(solvex)+1)=intersectpositivex(i);
    end
end
t=0;
intersectnegativex=[];
for i=1:length(ratsnegativex)
    tempnegativex(i)=ratsnegativex(i);
    if i>=2
        if tempnegativex(i)==tempnegativex(i-1)
            t=t+1;
            intersectnegativex(t)=ratsnegativex(i);
        end
    end
end
if isempty(intersectnegativex)==0
    for i=1:length(intersectnegativex)
        solvex(length(solvex)+1)=intersectnegativex(i);
        solvex(length(solvex)+1)=intersectnegativex(i);
    end
end
intersectx=intersect(ratspositivex,ratsnegativex);
if isempty(intersectx)==0
    if isempty(solvex)==0
        for i=1:length(intersectx)
            solvex(length(solvex)+1)=intersectx(i);
        end
    end
    if isempty(solvex)==1
        solvex(1)=intersectx(1);
        for i=2:length(intersectx)
            solvex(length(solvex)+1)=intersectx(i);
        end
    end
end
for i=1:length(ratspositivex)
    if imag(subs(fx,x,ratspositivex(i)-10e5*eps))~=0
        if subs(fx,x,ratspositivex(i)+eps)>0&&signx==">"||subs(fx,x,ratspositivex(i)+eps)<0&&signx=="<"
            solvex(length(solvex)+1)=ratspositivex(i);
        end
    end
    if imag(subs(fx,x,ratspositivex(i)+10e5*eps))~=0
        if subs(fx,x,ratspositivex(i)-eps)>0&&signx==">"||subs(fx,x,ratspositivex(i)-eps)<0&&signx=="<"
            solvex(length(solvex)+1)=ratspositivex(i);
        end
    end
end
for i=1:length(ratsnegativex)
    if imag(subs(fx,x,ratsnegativex(i)-10e5*eps))~=0
        if subs(fx,x,ratsnegativex(i)+eps)>0&&signx==">"||subs(fx,x,ratsnegativex(i)+eps)<0&&signx=="<"
            solvex(length(solvex)+1)=ratsnegativex(i);
        end
    end
    if imag(subs(fx,x,ratsnegativex(i)+10e5*eps))~=0
        if subs(fx,x,ratsnegativex(i)-eps)>0&&signx==">"||subs(fx,x,ratsnegativex(i)-eps)<0&&signx=="<"
            solvex(length(solvex)+1)=ratsnegativex(i);
        end
    end
end
solvex=sort(solvex);
t=1;
if signx=="<"
    t=-t;
end
rootplusx=subs(fx,x,min(solvex)+10^5*eps);
rootminusx=subs(fx,x,min(solvex)-10^5*eps);
rootplusrealx=0;
rootminusrealx=0;
if imag(rootplusx)==0
    rootplusrealx=vpa(rootplusx,5);
end
if imag(rootminusx)==0
    rootminusrealx=vpa(rootminusx,5);
end
if rootplusrealx>0||rootminusrealx<0
    t=-t;
end
for i=1:length(solvex)
    if t==1
        if i==1
            if 1==length(solvex)
                set=['(-inf,',char(solvex(i)),')'];
            end
            if 1~=length(solvex)
                set=['(-inf,',char(solvex(i)),')U'];
            end
        end
        if i~=1
            if i==length(solvex)
                set=[set,char(solvex(i)),')'];
            end
            if i~=length(solvex)
                set=[set,char(solvex(i)),')U'];
            end
        end
    end
    if t==-1
        if i==length(solvex)
            if i==1
                set=['(',char(solvex(i)),',+inf)'];
            end
            if i~=1
                set=[set,'(',char(solvex(i)),',+inf)'];
            end
        end
        if i~=length(solvex)
            if i==1
                set=['(',char(solvex(i)),','];
            end
            if i~=1
                set=[set,'(',char(solvex(i)),','];
            end
        end
    end
    if i~=-1
        t=-t;
    end
end
if isempty(solvex)==1
    if (subs(fx,x,1)>0||subs(fx,x,-1)>0)&&signx==">"||(subs(fx,x,1)<0||subs(fx,x,-1)<0)&&signx=="<"
        set="R";
    end
    if (subs(fx,x,1)>0||subs(fx,x,-1)>0)&&signx=="<"||(subs(fx,x,1)<0||subs(fx,x,-1)<0)&&signx==">"
        set="Empty";
    end
end
end