Ams
Ams

Reputation: 347

How to use interp1 function in matlab on a function of type symfun?

I have solutions to a system of differential equations in my matlab script. I define my functions as follows:

syms V_x(t) V_y(t) X(t) Y(t);
ode_V_x = diff(V_x,t) == -(B/m)*V_x;
ode_V_y = diff(V_y,t) == -g - (B/m)*V_y;
ode_X = diff(X,t) == V_x;
ode_Y = diff(Y,t) == V_y;

Then I solve them with proper initial conditions as follows:

[V_xSol(t), V_ySol(t), XSol(t), YSol(t)] = dsolve(odes,conds);

for example the solution for Y(t) comes out to be:

YSol(t) = exp(-t/5)*((981*exp(t/5))/4 - 12891678040772023/35184372088832) - (981*t)/20 + 4262710785985975/35184372088832

Now I need to find the value of time for which Y(t) = 0. I thought about using interp1 function in matlab by executing: t_f = interp1([0,5],YSol,0); but it doesnt work. The error reads: Values V must be of type double or single. What is the proper way of using interp1 on symfun type of function in matlab? P.S. independent variable 't' has not been defined as vector of anything prior persay (I understand matlab likes vectors).

Upvotes: 0

Views: 449

Answers (1)

decardinb
decardinb

Reputation: 170

I am not sure if the interpl function is the right tool for the job. You can use fsolve to achieve the result you want.

Usage of fsolve:

fsolve(function handle, initial guess)

This is implemented for your problem in matlab below:

t = fsolve(@(t)exp(-t/5)*((981*exp(t/5))/4 - 12891678040772023/35184372088832) - (981*t)/20 + 4262710785985975/35184372088832, 5)

The answer, in this case, is 4.3243. Take note that the answer depends on the initial guess that you give to the solver. In your case, I noticed any initial value above 2 should work. Anything otherwise will converge to a different/wrong answer.

You can read more about fsolve here: https://www.mathworks.com/help/optim/ug/fsolve.html

Upvotes: 2

Related Questions