Returning solution of system of equation as NaN solution
Following code is trying to find the solution of t and k by solving two equation. Matlab keeps returning NaN. What’s the issue?
% Define the symbolic variables
syms k t m dt_dk
% Define the main expression with t as a function of k
expr = ((1 – k)^(1/2)*(t^6*(2*m*(3*m + 2) + 4) – t^5*(m*(m*(5*m + 6) + 11) + 12) + m^6 + 2*t^7 – t^8 – t^4*(m*(3*m*(m*(3*m + 2) – 3) – 16) – 9) – 2*m^3*t^2*(3*m^3 + 2*m + 3) + m^2*t^3*(m*(3*m*(5*m + 4) + 1) – 4)) + k^(1/2)*(t^4*(m*(9*m*(m*(m + 4) + 5) + 28) + 9) – m^4*(3*m – 2) – t^6*(6*m*(m + 2) + 10) + t^5*(4*m^3 + 12*m + 8) + t^8 + m*t^2*(m*(m*(4*m^3 + 3*m + 9) + 6) – 8) + 2*m^2*t*(3*m – 2) – 2*m*t^3*(m*(m*(6*m*(m + 2) + 7) + 6) – 2))) / (18*t^3*(2*t – (m – t)^2));
% Derivative of t with respect to k (symbolically)
dt_dk_expr = diff(t, k);
% Differentiate the main expression with respect to k, taking into account dt/dk
diff_expr = diff(expr, k) + diff(expr, t) * dt_dk;
% Define the fixed-point equation
fixed_point_eq = (((k)^(1/2))*(((m/t)+2)/3) – ((1-k)^(1/2))*((1/3)*(2+ (m/t)+((2*m+t)/(((m-t)^2)-2*t)))))/(((k)^(1/2))*(2*(m^3) – 3*((1+m)^2)*t + t^3)/(3*(((m-t)^2)-2*t)) – ((1-k)^(1/2))*((2*m+t)/3)) – t;
% Define the range of m values
m_values = linspace(0.01, 0.99, 100); % Adjusted range to avoid edge cases (0 and 1)
% Preallocate arrays to store solutions
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
% Loop over m values and solve the system of equations for each m
for i = 1:length(m_values)
m_value = m_values(i);
% Substitute the current m value into the equations
diff_eq_sub = subs(diff_expr, m, m_value);
fixed_eq_sub = subs(fixed_point_eq, m, m_value);
% Solve the system of equations
% Note: vpasolve automatically adjusts precision based on the symbolic nature
sol = vpasolve([diff_eq_sub == 0, fixed_eq_sub], [k, t, dt_dk]);
% Extract solutions
if ~isempty(sol.k) && ~isempty(sol.t)
k_solutions(i) = double(sol.k);
t_solutions(i) = double(sol.t);
else
k_solutions(i) = NaN;
t_solutions(i) = NaN;
end
end
% Display solutions
disp(‘Solutions for each m:’);
for i = 1:length(m_values)
fprintf(‘m = %.4f, k = %.4f, t = %.4fn’, m_values(i), k_solutions(i), t_solutions(i));
end
% Plot solutions
figure;
plot(m_values, k_solutions, ‘-o’, ‘DisplayName’, ‘k’);
hold on;
plot(m_values, t_solutions, ‘-o’, ‘DisplayName’, ‘t’);
xlabel(‘m’);
ylabel(‘Values’);
title(‘Solutions as a Function of m’);
legend(‘k’, ‘t’);
hold off;Following code is trying to find the solution of t and k by solving two equation. Matlab keeps returning NaN. What’s the issue?
% Define the symbolic variables
syms k t m dt_dk
% Define the main expression with t as a function of k
expr = ((1 – k)^(1/2)*(t^6*(2*m*(3*m + 2) + 4) – t^5*(m*(m*(5*m + 6) + 11) + 12) + m^6 + 2*t^7 – t^8 – t^4*(m*(3*m*(m*(3*m + 2) – 3) – 16) – 9) – 2*m^3*t^2*(3*m^3 + 2*m + 3) + m^2*t^3*(m*(3*m*(5*m + 4) + 1) – 4)) + k^(1/2)*(t^4*(m*(9*m*(m*(m + 4) + 5) + 28) + 9) – m^4*(3*m – 2) – t^6*(6*m*(m + 2) + 10) + t^5*(4*m^3 + 12*m + 8) + t^8 + m*t^2*(m*(m*(4*m^3 + 3*m + 9) + 6) – 8) + 2*m^2*t*(3*m – 2) – 2*m*t^3*(m*(m*(6*m*(m + 2) + 7) + 6) – 2))) / (18*t^3*(2*t – (m – t)^2));
% Derivative of t with respect to k (symbolically)
dt_dk_expr = diff(t, k);
% Differentiate the main expression with respect to k, taking into account dt/dk
diff_expr = diff(expr, k) + diff(expr, t) * dt_dk;
% Define the fixed-point equation
fixed_point_eq = (((k)^(1/2))*(((m/t)+2)/3) – ((1-k)^(1/2))*((1/3)*(2+ (m/t)+((2*m+t)/(((m-t)^2)-2*t)))))/(((k)^(1/2))*(2*(m^3) – 3*((1+m)^2)*t + t^3)/(3*(((m-t)^2)-2*t)) – ((1-k)^(1/2))*((2*m+t)/3)) – t;
% Define the range of m values
m_values = linspace(0.01, 0.99, 100); % Adjusted range to avoid edge cases (0 and 1)
% Preallocate arrays to store solutions
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
% Loop over m values and solve the system of equations for each m
for i = 1:length(m_values)
m_value = m_values(i);
% Substitute the current m value into the equations
diff_eq_sub = subs(diff_expr, m, m_value);
fixed_eq_sub = subs(fixed_point_eq, m, m_value);
% Solve the system of equations
% Note: vpasolve automatically adjusts precision based on the symbolic nature
sol = vpasolve([diff_eq_sub == 0, fixed_eq_sub], [k, t, dt_dk]);
% Extract solutions
if ~isempty(sol.k) && ~isempty(sol.t)
k_solutions(i) = double(sol.k);
t_solutions(i) = double(sol.t);
else
k_solutions(i) = NaN;
t_solutions(i) = NaN;
end
end
% Display solutions
disp(‘Solutions for each m:’);
for i = 1:length(m_values)
fprintf(‘m = %.4f, k = %.4f, t = %.4fn’, m_values(i), k_solutions(i), t_solutions(i));
end
% Plot solutions
figure;
plot(m_values, k_solutions, ‘-o’, ‘DisplayName’, ‘k’);
hold on;
plot(m_values, t_solutions, ‘-o’, ‘DisplayName’, ‘t’);
xlabel(‘m’);
ylabel(‘Values’);
title(‘Solutions as a Function of m’);
legend(‘k’, ‘t’);
hold off; Following code is trying to find the solution of t and k by solving two equation. Matlab keeps returning NaN. What’s the issue?
% Define the symbolic variables
syms k t m dt_dk
% Define the main expression with t as a function of k
expr = ((1 – k)^(1/2)*(t^6*(2*m*(3*m + 2) + 4) – t^5*(m*(m*(5*m + 6) + 11) + 12) + m^6 + 2*t^7 – t^8 – t^4*(m*(3*m*(m*(3*m + 2) – 3) – 16) – 9) – 2*m^3*t^2*(3*m^3 + 2*m + 3) + m^2*t^3*(m*(3*m*(5*m + 4) + 1) – 4)) + k^(1/2)*(t^4*(m*(9*m*(m*(m + 4) + 5) + 28) + 9) – m^4*(3*m – 2) – t^6*(6*m*(m + 2) + 10) + t^5*(4*m^3 + 12*m + 8) + t^8 + m*t^2*(m*(m*(4*m^3 + 3*m + 9) + 6) – 8) + 2*m^2*t*(3*m – 2) – 2*m*t^3*(m*(m*(6*m*(m + 2) + 7) + 6) – 2))) / (18*t^3*(2*t – (m – t)^2));
% Derivative of t with respect to k (symbolically)
dt_dk_expr = diff(t, k);
% Differentiate the main expression with respect to k, taking into account dt/dk
diff_expr = diff(expr, k) + diff(expr, t) * dt_dk;
% Define the fixed-point equation
fixed_point_eq = (((k)^(1/2))*(((m/t)+2)/3) – ((1-k)^(1/2))*((1/3)*(2+ (m/t)+((2*m+t)/(((m-t)^2)-2*t)))))/(((k)^(1/2))*(2*(m^3) – 3*((1+m)^2)*t + t^3)/(3*(((m-t)^2)-2*t)) – ((1-k)^(1/2))*((2*m+t)/3)) – t;
% Define the range of m values
m_values = linspace(0.01, 0.99, 100); % Adjusted range to avoid edge cases (0 and 1)
% Preallocate arrays to store solutions
k_solutions = zeros(size(m_values));
t_solutions = zeros(size(m_values));
% Loop over m values and solve the system of equations for each m
for i = 1:length(m_values)
m_value = m_values(i);
% Substitute the current m value into the equations
diff_eq_sub = subs(diff_expr, m, m_value);
fixed_eq_sub = subs(fixed_point_eq, m, m_value);
% Solve the system of equations
% Note: vpasolve automatically adjusts precision based on the symbolic nature
sol = vpasolve([diff_eq_sub == 0, fixed_eq_sub], [k, t, dt_dk]);
% Extract solutions
if ~isempty(sol.k) && ~isempty(sol.t)
k_solutions(i) = double(sol.k);
t_solutions(i) = double(sol.t);
else
k_solutions(i) = NaN;
t_solutions(i) = NaN;
end
end
% Display solutions
disp(‘Solutions for each m:’);
for i = 1:length(m_values)
fprintf(‘m = %.4f, k = %.4f, t = %.4fn’, m_values(i), k_solutions(i), t_solutions(i));
end
% Plot solutions
figure;
plot(m_values, k_solutions, ‘-o’, ‘DisplayName’, ‘k’);
hold on;
plot(m_values, t_solutions, ‘-o’, ‘DisplayName’, ‘t’);
xlabel(‘m’);
ylabel(‘Values’);
title(‘Solutions as a Function of m’);
legend(‘k’, ‘t’);
hold off; nan, optimization, differential equations MATLAB Answers — New Questions