Hello everybody,
I'd like to solve for y = y(x) the following equation
d log( y ) / d x + y = 1 + f
with f = f(x).
f is a 1D numerically known array, I don't know its nalytical form.
I cannot set properly solve or fzero.
Can you help me, please?
Patrizio

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 14일
편집: Ameer Hamza 2020년 6월 14일

2 개 추천

This is a differential equation and you can use symbolic toolbox to find an anayltical solution
syms y(x) f
eq = diff(log(y), x) + y == 1 + f;
sol = dsolve(eq);
Result
>> sol
sol =
(exp((C1 + x)*(f + 1))*(f + 1))/(exp((C1 + x)*(f + 1)) + 1)
f + 1
Following shows how to get a numerical solution using ode45
syms y(x) f
eq = diff(log(y), x) + y == 1 + f;
sol = dsolve(eq);
odeFun = matlabFunction(odeToVectorField(eq), 'Vars', {'t', 'Y', 'f'});
tspan = [0 10]; % time span for numerical solution
ic = 1; % initial condition: y(0)==1
fv = 1; % numerical solution for f=1
[t, y] = ode45(@(t, y) odeFun(t, y, fv), tspan, ic);
plot(t, y);

댓글 수: 7

Thank you Ameer!
In the system I'm stuying, for each 'x' value there is one value for 'f' and one value for 'y'.
I mean, there is a f(x) that I know and a y(x) I don't know but for each 'x' value there are only one 'f' and one 'y' . I know f(x) and the relation between f(x) and y(x), and should find y(x).
As far as I understand, the tspan plays the role of the 'x' variable and I get a y(x) for each 'fv' but this not actually the case: for each 'x' value I have also a different 'fv' value.
Hope this makes my problem clearer. If I have misunderstood your answer, sorry.
About the symbolic toolbok lines you wrote, it looks to be the case for a scalar f.
if I input
syms y(x) f(x)
I get this
sol =
exp(int(f(x) + 1, x, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true))/(C6 - int(-exp(int(f(x) + 1, x, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true)), x, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true))
that I don't understand clearly. I wonder if you can hel me again, please.
Thanks
Patrizio
I get your point. Yes, tspan plays the role of 'x'. Even if f is a function of 'x', you can consider that in the above code, f(x)=1, i.e., it is a constant function. Although, I agree with your point that in the general case, f and y need to vary with 'x'. Do you have a specific expression for f(x)? Solving it using dsolve() for general case f(x) might not give any useful results.
Hi Ameer, thanks!
Unfortunately f(x) is an experimental measurements and I don't have a precise analytical expression for it. I would need of y(x) to fit it and disentangle between two physical possibilities...
Anyway, I'l keep thinking about.
Ameer Hamza
Ameer Hamza 2020년 6월 14일
편집: Ameer Hamza 2020년 6월 14일
If f is a function of 'x' but obtained experimentally, then you can use the solution in my answer by considering 'f' as a symbolic variable. Following shows how it can be done
syms y(x) f
eq = diff(log(y), x) + y == 1 + f;
sol = dsolve(eq);
odeFun = matlabFunction(odeToVectorField(eq), 'Vars', {'t', 'Y', 'f'});
xspan = 0:0.1:10; % time span for numerical solution
ic = 1; % initial condition: y(0)==1
fv = rand(size(xspan));
ffun = @(x) interp1(xspan, fv, x);
[x, y] = ode45(@(x, y) odeFun(t, y, ffun(x)), xspan, ic);
plot(x, y);
Here I used xspan as a vector, and the values of function f (fv) are also available for the values in xspan. I generated fv randomly in this code.
Thank you so much!!!
It will take to me a few days to test it, I'll keep you posted.
Hi Ameer,
you're solution is brilliant!
Sorry that I couldn't test it before... Your support has been excellent!
In the case we publish the data analysis done thanks to your solution, we'll aknowledge your support.
Thanks
Patrizio
I am glad that it worked for your case, and you got the results. Good luck with your research.

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

제품

질문:

2020년 6월 14일

댓글:

2020년 6월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by