Conversion to logical from sym is not possible.

3 views (last 30 days)
clc
clear
syms t u r a b c d x y j v t Output STP RMP
p= input('Number of intervals of the signal (Integer value):');
n=0;
a=input('Enter signal starting time:');
b=input('Enter signal function end time:');
while n<p;
double c=input('Enter piecewise initial value:');
double d=input('Enter piecewise final value:');
double x=input('Enter piecewise function lower interval:');
double y=input('Enter piecewise function upper interval:');
double j=input('Enter piecewise function slope:');
double v(t)=input('Enter piecewise function:');
double t=x:y;
if c==d
STP(t)=c*u*ustep(t,-x);
else
RMP(t)=r*ramp(t,j,c);
end
n=n+1;
end
Output=STP+RMP
function y = ramp(t,m,ad)
% ramp generation
% t: time support
% m: slope of ramp
% ad : advance (positive), delay (negative) factor
% USE: y = ramp(t,m,ad)
N = length(t);
y = zeros(1,N);
for i = 1:N,
if t(i)> = -ad,
y(i) = m*(t(i) + ad);
end
end
function y = ustep(t,ad)
% generation of unit step
% t: time
% ad : advance (positive), delay (negative)
% USE y = ustep(t,ad)
N = length(t);
y = zeros(1,N);
for i = 1:N,
if t(i)> = -ad,
y(i) = 1;
end
end
I get this error
Conversion to logical from sym is not possible.
Error in ramp (line 10)
if t(i)>=-ad;
Error in Expriments (line 20)
RMP(t)=ramp(t,j,x);
In general, I am trying to get output as u(t)+r(t) (ramp + step function) and I am stuck at this point.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2020
double c=input('Enter piecewise initial value:');
That means the same thing to MATLAB as
double( 'c=input(''Enter piecewise initial value:'')') ;
which takes a character vector and outputs the vector of character codes corresponding to each character and then throws them away because of the semi-colon. This is an example of "command/function duality"
Because of this you are not assigning to c in that line so c retains its sym declaration that you started with.
  4 Comments
Osama Aliwat
Osama Aliwat on 7 Nov 2020
Again thank you and as you predicted , t is not always an integer so I completely removed that statement similarly for rmp(t). The thing is I am trying to compute every value of RMP and STP of the loop. After some adjustments, this is my code for now
clc
clear
syms r u t Output
p= input('Number of intervals of the signal (Integer value):');
n=0;
a=input('Enter signal starting time:');
b=input('Enter signal function end time:');
while n<p;
c=input('Enter piecewise initial value:');
d=input('Enter piecewise final value:');
x=input('Enter piecewise function lower interval:');
y=input('Enter piecewise function upper interval:');
j=input('Enter piecewise function slope:');
v=input('Enter piecewise function:');
if c==d
STP=c*u*ustep(t,x);
else
RMP=r*ramp(t,j,x);
end
n=n+1;
end
Output=STP+RMP
and I get this error
Conversion to logical from sym is not possible.
Error in ramp (line 10)
if t(i)>=-ad;
Error in Expriments (line 20)
RMP=r*ramp(t,j,x);
Walter Roberson
Walter Roberson on 8 Nov 2020
You have some choices to make:
  • you can use the Symbolic Toolbox to construct piecewise() functions based on symbolic times; use an "otherwise" of 0 so you can add the parts together. Later you can fplot() the result, selecting whatever range of times you want. But you need to confirm that if the user enters an overlap that you want the sum of the results in that section, as compared to wanting the oldest condition to hold, or the newest condition (either of those can be arranged without too much effort)
  • you can use a fixed sampling frequency and generate samples from 0 to the discretized version of the largest time entered, and generate numeric values at each of the times. But you need to confirm that if the user enters an overlap that you want the sum of the results in that section, as compared to wanting the oldest condition to hold, or the newest condition (either of those can be arranged without too much effort). If you take this approach, you cannot later zoom in to finer than the original sampling interval. You also have to figure out what to do about transitions that happen within one interval -- for example if you sample at 100 Hz and the user enters 0.007 as the starting time, then do you form a weighted average of 7/10 times the old value (probably 0) for the interval + 3/10 times the new value for the interval, or do you blindly sum the old and new value as if the function applied 100% for the interval?
  • for each interval, you can generate a vector of numeric times, that might effectively be at a different sampling frequency for each interval, and corresponding numeric results. You can then slam all the interval times together with NaN between the parts, and all of the results together with NaN between the parts, and plot it. If you take this approach, you cannot later zoom in to finer than the sampling rate for the interval. Also, because the user might overlap the intervals, you could get multiple line segments for the same time. So you might instead want to take all those arrays and find the union of the times, and interp1() all of the intervals against the union of times, with extrapolation set to 0, and add all of the results together, which would have the effect of rationalizing down to a single sample per time. ut you need to confirm that if the user enters an overlap that you want the sum of the results in that section, as compared to wanting the oldest condition to hold, or the newest condition (either of those can be arranged)

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!