MATLAB Answers

How to plot graph of value of variable when a parameter is varied

13 ビュー (過去 30 日間)
Teck Lim
Teck Lim 2020 年 4 月 9 日
コメント済み: Ameer Hamza 2020 年 4 月 9 日
Hi all,
I have a system of equations and I would like to solve for e, τ, and k. However, I want to assume that the parameter R can take on several different values (e.g. 0.5, 1, 1.5). In particular, I hope to plot a graph of how the solution for any of my variables e, τ or k varies with R. I am really struggling with how this can be implemented in MATLAB. Any help will be much appreciated.

  0 件のコメント

Sign in to comment.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
編集済み: Ameer Hamza 2020 年 4 月 9 日
Write your system of equation in the form
Where , and are the three equations in your queston. Then apply fsolve() function.
This is the code:
f_1 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - 0.336*e.^-0.3./(tau+R.*k);
f_2 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - 0.5./tau;
f_3 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - (R-0.3)./(tau+R.*k);
f = @(e, tau, k, R) [f_1(e,tau,k,R); f_2(e,tau,k,R); f_3(e,tau,k,R)];
R = [0.5, 1, 1.5];
sol = zeros(numel(R), 3);
for i=1:numel(R)
sol(i,:) = fsolve(@(x) f(x(1),x(2),x(3), R(i)), rand(1,3));
end
plot(R, sol);
legend({'e', 'tau', 'k'});

  5 件のコメント

表示 2 件の古いコメント
Teck Lim
Teck Lim 2020 年 4 月 9 日
For my pen and paper calculations, I assumed R=1 and I obtained:
τ=0.01698
e=0.0866
k=0.00679
Certainly, I am not ruling out multiple solutions. How would the algorithm respond if that was the case? Does that mean the solution will depend on the initial guess?
Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
Teck, your equation does seem to have multiple solutions, and the solution you mentioned for R=1 is correct. I tried different initial points, and values of R. The fsolve() is able to find a solution if R >= 0.8, but for smaller values, it cannot find a solution
f_1 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - 0.336*e.^-0.3./(tau+R.*k);
f_2 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - 0.5./tau;
f_3 = @(e, tau, k, R) 1./(0.8*e.^0.7-e-tau-k) - (R-0.3)./(tau+R.*k);
f = @(e, tau, k, R) [f_1(e,tau,k,R); f_2(e,tau,k,R); f_3(e,tau,k,R)];
R = [0.5, 0.8, 1, 1.5, 2];
sol = zeros(numel(R), 3);
for i=1:numel(R)
sol(i,:) = fsolve(@(x) f(x(1),x(2),x(3), R(i)), 1e-6.*rand(1,3));
f(sol(i,1), sol(i,2), sol(i,3), R(i)) % to check if the 3 equations are true.
end
plot(R, sol);
legend({'e', 'tau', 'k'});
Teck Lim
Teck Lim 2020 年 4 月 9 日
Thanks again for your time.

Sign in to comment.

その他の回答 (1 件)

Torsten
Torsten 2020 年 4 月 9 日
Your equations can only be solved for one value of R, namely R = 0.3 + 0.366*exp(-0.3).
This can be seen by dividing equation 1 by equation 3.

  4 件のコメント

表示 1 件の古いコメント
Torsten
Torsten 2020 年 4 月 9 日
ok, then at least we can explicitly solve for e depending on R.
Taking the reciprocals of all the 6 expressions and inserting the result for e from the first step, we can solve for tau and k since we have a linear system of equations.
Teck Lim
Teck Lim 2020 年 4 月 9 日
Thanks for the suggestion. I will try that out.
Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
Torsten's analysis is correct. This system does have a closed-form solution. I used a symbolic toolbox to solve this system of equation, and it gives more accurate results (as expected) at R=0.5 as compared to the numeric solver. Following code shows the solution with symbolic approach
syms e tau k R
eq1 = 1./(0.8*e.^0.7-e-tau-k) - 0.336*e.^-0.3./(tau+R.*k);
eq2 = 1./(0.8*e.^0.7-e-tau-k) - 0.5./tau;
eq3 = 1./(0.8*e.^0.7-e-tau-k) - (R-0.3)./(tau+R.*k);
r = [0.5 1 1.5];
sols = zeros(numel(r), 3);
for i=1:numel(r)
sol = solve([eq1==0 eq2==0 eq3==0], [e tau k]);
sols(i,1) = subs(sol.e(1), R, r(i));
sols(i,2) = subs(sol.tau(1), R, r(i));
sols(i,3) = subs(sol.k(1), R, r(i));
end
sols = double(sols);
plot(r, sols);
legend({'e', 'tau', 'k'});

Sign in to comment.

製品


Translated by