# how to uses str2sym

28 views (last 30 days)
Rim SALAH on 10 Nov 2020
clear all;clc
disp('------');
disp('The Nonlinear systems should be written in thefollowing form');
disp('Sliding Mode Controller for a class of Nonlinear systems');
disp(' State space equations x=f(x)+gu ');
disp('-----------');
n=input('Number of states:=');
nin=input('Number of inputs:=');
x=sym(zeros(1,n));
u=sym(zeros(1,nin));
par=input('Parameters ','s');
eval(sprintf('syms %s',par));
parameters=sprintf('%s',par);
for j=1:n
eval(sprintf('syms x%d',j))
x(:,j)=sprintf('x%d',j);
end
for k=1:nin
eval(sprintf('syms u%d',k));
u(:,k)=sprintf('u%d',k);
end
syms u
f=input('The vector f(x):=','s');
g=input('The vector g(x):=','s');
Hc=input('The output variables:=','s');
[Lhf,Lhg,dh,L,u,r]=NonContSidFed(f,g,Hc,x,u);
[Surf,dSurf,dd,K,Uc]=SlidingModeTerms(Hc,L,r,Lhg);

Pranav Verma on 13 Nov 2020
Hi Rim,
The str2sym function takes in a symbolic expression in a string form as input & evaluates it. Please refer to the below example illustrating the same:
>> st = '2+2';
>> str2sym(st)
ans =
4
Thanks

Shubham Kumar Thakur on 2 Apr 2021
syms t k;
ft1=input('Enter function of t (First part): ');
ft2=input('Enter function of t (Second part): ');
ft1= str2sym (ft1)
ft2= str2symm(ft2)
N=input('Enter No. of harmonics to be calculated: ');
t1=inpit('Enter first limit: ');
t2=inpit('Enter second limit: ');
t3=inpit('Enter third limit: ');
T=t3-t1; %Time Peiod
w0=(2*pi)/T; %Frequency
n=-N;1;N; %number of Harmonics (both sides)
Fn=(1/T)*(int((ft1*exp(-1j*n*w0*t)),t1,t2)+int((ft2*exp(-1j*n*w0*t)),t2,t3));
expar=exp(1j*w0*n*t);
tx=t1:0.001:t3; %Time vector for plotting
t=tx(:,1:end-1); nop=length(t); %no. of point in t
%plotting Input function
nop1=find(tx==t2)-find(tx==t1); nop2=find(tx==t3)-find(tx==t2);
t=tx(1:nop1); ftt1=eval(ft1).*ones(1,nop1);
t=tx(nop2+1:nop); ftt2=eval(ft2).*ones(1,nop2);
ft0=[ftt1 ftt2];
t=tx{:,1:end-1};