octave - Plotting composition of two functions in matlab -
i can't understand wrong code here in matlab:
syms t x=[1 2 3]; g=t*t; f=sin(x); y=compose(g,f); plot(x,y,'o') this code wrote plot y=sin(x)^2 .as don't have matlab in computer running on octave online compiler , giving error 'syms' undefined near line 1 column 1.
please if can help explaining wrong in code?
it looks don't have symbolic math toolbox installed can't utilize compose. there way around though. remember, composition of 2 functions when calling compose(f,g) such homecoming f(g(y)) f = f(x) , g = g(y).
what can plot numerically, without relying on syms. declare numeric time vector is... say... t = 0 t = 5 in steps of 0.01. after, declare own function handles compute each function. function handles or anonymous functions little one-liner functions mathematical or computational statements can take input of size, whether single value, vector or matrix , applies computational statement every value in input. signify anonymous function @ symbol, within round brackets, specify variables compose of statement want compute. in case, there single variable phone call t, can variable really.
therefore, define function g performs t^2 every element in our input. note have element-by-element operations. if did t*t, should provide vector or matrix, interpreted matrix multiplication , not want. similarly, define anonymous function f such produces sin(t) output.
with these defined, need is:
t = 0 : 0.01 : 5; g = @(t) t.*t; f = @(t) sin(t); y = g(f(t)); %// composition of 2 functions (compose(g,f)); plot(t, y, 'o'); matlab octave matlab-figure
No comments:
Post a Comment