solving trascendental equations, proper setting
显示 更早的评论
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
2020-6-14
编辑:Ameer Hamza
2020-6-14
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.
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 中查找有关 Symbolic Math Toolbox 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
