How can I declare an implicit variable, y(t), after I have used it in a symbolically defined function?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm writing a scheme that solves an ODE and in doing so I declare a symbolic function:
syms t y(t)
f = t^(-2) * (sin(2*t) - 2*t*y)
At one point I have to take a total derivative, so I need to be be implicit in the expression, but I also need to evalute at a specific value of t and y. I attempt to do this in the following lines.
y(t) = wi;
out(1) = f(ti);
However, I am prompted with the following error.
The following error occurred converting from sym to double:
Unable to convert expression containing remaining symbolic
function calls into double array. Argument must be
expression that evaluates to number.
Error in DiffEq_alt (line 13)
out(1) = f(ti);
Ultimately, I need to be able to be able to declare y explicitly, but also treat it implicitly for when I differentiate the function with respect to t.
0 Kommentare
Antworten (1)
Nipun
am 22 Mai 2024
Hi David,
I understand that you are trying to solve an ordinary differential equation (ODE) using symbolic mathematics in MATLAB, and you encounter an issue when attempting to evaluate a symbolic expression at specific numerical values for t and y(t). You want to keep y as a function of t for differentiation purposes but also need to substitute specific values for t and y(t).
To achieve this, you can use subs to substitute specific values into your symbolic expression before converting it to a numerical value. Here's how you can adjust your code to avoid the error:
To substitute a specific value for t and y(t), use the subs function. Let us say you want to evaluate f at t = ti and y(t) = wi. You can do this as follows:
ti = 1; % Example value for t
wi = 0.5; % Example value for y(t)
% Substitute the specific values into f
f_sub = subs(f, [t, y(t)], [ti, wi]);
After substituting the specific values, you can convert the result to a numerical value using double:
out(1) = double(f_sub);
This approach allows you to keep y as a function of t for differentiation and other symbolic operations, while also being able to evaluate f at specific numerical values for t and y(t).
Here is the combined snippet:
syms t y(t)
f = t^(-2) * (sin(2*t) - 2*t*y);
ti = 1; % Specific value for t
wi = 0.5; % Specific value for y(t)
% Substitute specific values into f and convert to numerical value
f_sub = subs(f, [t, y(t)], [ti, wi]);
out(1) = double(f_sub);
This should resolve the error and allow you to evaluate your symbolic expression at specific numerical values.
Hope this helps.
Regards,
Nipun
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!