Vector substitution into symbolic function

32 visualizaciones (últimos 30 días)
noMathWiz
noMathWiz el 20 de Jul. de 2020
Respondida: Walter Roberson el 20 de Jul. de 2020
Matlab gurus,
I need your help with vector substitution into a symblolic function and how to do this with element-wise substitutiton. My code that I'm working with so far has two problems that I can't figure out:
  • How to substitute a vector of values (one variable) into a symbolic function that has that same variable in it
  • How to do elementwise division when the symbolic function contains division operators without the '.' elementwise built into the matlab generated.
Thank you!
% define constants
A1 = 0.696749;
A2 = 0.408218;
A3 = 0.890815;
l1 = 0.0690660e-6;
l2 = 0.115662e-6;
l3 = 9.900559e-6;
c = 3e8;
% define symbolic variables
syms n(l) l A B C
% describe symbolic functions
A = (A1.*(l.^2))./(l.^2 - l1^2);
B = (A2.*(l.^2))./(l.^2 - l2^2);
C = (A3.*(l.^2))./(l.^2 - l3^2);
% Define a compositte symbolic function that we want to differentiate:
n(l) = sqrt(1 + A + B + C);
% Take the second derivative of te function n(l) with respect to
d2n = diff(n,l,2);
% Use vpa?????
d2n_dl2 = vpa(d2n);
% define a vector to substitue in for the unknown l
L = linspace(1460-9,1530e-9,1000);
%substitue the vector lam for l:
d2n_dl2subs = subs(d2n_dl2,l,L)
% NOTE: This neees to be real vector after calculating this line (i.e. NOT symbolic).
% Also, since d2n_dl2 contains division, this needs to be done elmement wise
% calculate a new vector based on this substitution.
Dm = (c./L) .* d2n_dl2subs; % this also needs to be a real vector, not symbolic.

Respuestas (2)

madhan ravi
madhan ravi el 20 de Jul. de 2020
Editada: madhan ravi el 20 de Jul. de 2020
d2n_dl2 = matlabFunction(d2n);
d2n_dl2subs = d2n_dl2(L)

Walter Roberson
Walter Roberson el 20 de Jul. de 2020
How to substitute a vector of values (one variable) into a symbolic function that has that same variable in it
subs() on the symbolic function works. However, as you might have noticed, the result is a symbolic function that returns a vector of values, which is typically not what you want.
When you have a symbolic function and you want to replace a variable with a vector, then there are typically distinct cases:
  1. The variable being replaced is the only symbolic variable in the function. In this case, apply the function to the variable, getting out a vector of values. Example: d2n_dl2(L) . This is probably what you want in this case;
  2. The variable being replaced is not the only variable in the function, but the other variables will all only be replaced by scalars. In this case, it can make sense sometimes to subs() the value in for the variable in the function, getting back a function that returns a vector; later you would apply the function to scalar inputs getting back a vector of values. However if you need to index the values before applying the function (such as to find the expression that corresponds to the third value of the vector substituted in), then you should instead turn the function into its corresponding expression by evaluating it on variables with the same name as its arguments, getting out an expression instead of a function: you can index an expression but you cannot index a function.
  3. The variable being replaced is not the only variable in the function, and at least one of the other variables will be replaced by a non-scalar. In this case, most of the time it is wrong to substitute in the vector at this stage: most of the time you should hold off and do a simultaneous substitution using subs(expression, {var1, var2, ...}, {value1, value2, value3}) form. This is the approach you should use when you need to have corresponding elements matched up

Community Treasure Hunt

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

Start Hunting!

Translated by