How to replace a variable or function with another variable or function ?

조회 수: 10 (최근 30일)
Pavan Bharadwaj
Pavan Bharadwaj 2016년 5월 25일
편집: Parth Shrimali 2020년 12월 1일
for example. I have a function
y = x*(x^2 - 1)
In this function, I want to replace x with cos(t)
the required function is,
y = cos(t) ( (cos(t))^2 - 1)

답변 (3개)

Are Mjaavatten
Are Mjaavatten 2016년 5월 25일
편집: Are Mjaavatten 2016년 5월 25일
You could define an anonymous function f(x):
f = @(x) x.*(x.^2-1);
Note the '.' before the * and ^operators. This lets the function work with array inputs. I use this feature in the example at the end.
Now you can use cos(t) as an argument to f:
t = 2;
y = f(cos(t))
Or you may define a new anonymous function g:
g = @(t) f(cos(t));
You may now try something like this:
t = linspace(0,2*pi);
plot(t,g(t))
You may want to take a look at the Matlab documentation for anonymous functions.
  댓글 수: 2
Pavan Bharadwaj
Pavan Bharadwaj 2016년 5월 25일
Thank you for your answer.
In the case I am working on, The result of the differential acts the function and in this function I want to replace x by cos(t). The resultant function has to again be differentiated with respect to t.
Cheers, Pavan
Are Mjaavatten
Are Mjaavatten 2016년 5월 25일
Pavan,
It seems from your comment that I may have misunderstood your question.
You mention 'differential' in your comment. Are you asking how to do some symbolic manipulations to the equations to yield a mathemathical differential (e.g. dy or dx), formulate a differential equation, or maybe how to find the derivative dy/dt?
If so, maybe the symbolic math toolbox may be of help. I do not have this toolbox, but if you state your problem more clearly, maybe someone else can give a relevant answer.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2016년 5월 25일
Since it seems like you want to perform symbolic computations, see the subs function in Symbolic Math Toolbox.

Parth Shrimali
Parth Shrimali 2020년 12월 1일
편집: Parth Shrimali 2020년 12월 1일
syms x y z;
p = 3*x + y +z;
% suppose you wanna replace 'x' with
% new variable 'a' in our difined
% variable 'p'
syms a;
subs(p,'x',a)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by