assemble it so that it will run

조회 수: 2 (최근 30일)
MINATI PATRA
MINATI PATRA 2024년 1월 11일
댓글: MINATI PATRA 2024년 1월 11일
% this function estimates the correct value of a for big Pr cases
function f=big_Pr(a)
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 7],x0);
% check the BC's at infinity approach zero
f(1)=Y(end,2);
f(2)=Y(end,4)^2+Y(end,5)^2;
end
% this function defines the similarity equations as defined by
% Bejan using correct scaling and Ostrach using incorrect scaling
function xdot=governing_equation(t,x)
global Pr
% Based on Bejan's scaling similarity equation
% The state-space derivation is shown in the document
xdot(1)=x(2);
xdot(2)=x(3);
xdot(3)=-(1/Pr)*(0.5*x(2)^2-(3*x(1)*x(3))/4)+x(4);
xdot(4)=x(5);
xdot(5)=3*x(1)*x(5)/4;
xdot=xdot';
% Based on Ostrach's scaling similarity equation
% % xdot(1)=x(2);
% % xdot(2)=x(3);
% % xdot(3)=-3*x(1)*x(3)+2*x(2)^2-x(4);
% % xdot(4)=x(5);
% % xdot(5)=-3*Pr*x(1)*x(5);
% % xdot=xdot';
end
global Pr
Pr = 1
options=optimset('TolFun',1e-6,'TolX',1e-6);
a=fsolve(@big_Pr,[0.5 -0.6],options);
a
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 4],x0);
figure(1);
plot(t,-Y(:,2),'r')
hold on
figure(2);
plot(t,Y(:,4),'r')
hold on
Pr=100
options=optimset('TolFun',1e-6,'TolX',1e-6);
a=fsolve(@big_Pr,[0.25 -0.7],options);
a
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 4],x0);
figure(1);
plot(t,-Y(:,2),'b')
figure(2);
plot(t,Y(:,4),'b')
Pr=1000
options=optimset('TolFun',1e-6,'TolX',1e-6);
a=fsolve(@big_Pr,[0.1 -0.65],options);
a
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 4],x0);
figure(1);
plot(t,-Y(:,2),'k')
figure(2);
plot(t,Y(:,4),'k')
Pr=10
options=optimset('TolFun',1e-6,'TolX',1e-6);
a=fsolve(@big_Pr,[0.5 -0.52],options);
a
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 4],x0);
figure(1);
plot(t,-Y(:,2),'g')
figure(2);
plot(t,Y(:,4),'g')
Pr=0.01
options=optimset('TolFun',1e-6,'TolX',1e-6);
a=fsolve(@small_Pr,[0.15 -0.17],options);
a
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 4],x0);
figure(1);
plot(t,-Y(:,2),'y')
legend('Pr = 1','Pr = 100','Pr = 1000','Pr = 10','Pr = 0.01')
title('Vertical Velocity Profiles')
xlabel('η = (x/y) Ra_y^(^0^.^2^5^)')
ylabel('G = v(y/α) Ra_y^(^0^.^2^5^)')
figure(2);
plot(t,Y(:,4),'y')
legend('Pr = 1','Pr = 100','Pr = 1000','Pr = 10','Pr = 0.01')
title('Temperature Profiles')
xlabel('η = (x/y) Ra_y^(^0^.^2^5^)')
ylabel('ϴ')
function f=small_Pr(a)
x0=[0 0 a(1) 1 a(2)];
[t,Y]=ode15s(@governing_equation,[0 30],x0);
f(1)=Y(end,2);
f(2)=Y(end,4)^2+Y(end,5)^2;
end
%% I want to run this code, please assemble it so that it will run in 2023b version

채택된 답변

Hassaan
Hassaan 2024년 1월 11일
clear
clc
close all
% Define global variable
global Pr
% Set the Prandtl number and solve for different cases
Pr = 1;
options = optimset('TolFun', 1e-6, 'TolX', 1e-6);
a = fsolve(@big_Pr, [0.5 -0.6], options);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 4], x0);
plot_results(t, Y, 'r', 'Pr = 1');
Pr = 100;
a = fsolve(@big_Pr, [0.25 -0.7], options);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 4], x0);
plot_results(t, Y, 'b', 'Pr = 100');
Pr = 1000;
a = fsolve(@big_Pr, [0.1 -0.65], options);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 4], x0);
plot_results(t, Y, 'k', 'Pr = 1000');
Pr = 10;
a = fsolve(@big_Pr, [0.5 -0.52], options);
Equation solved, inaccuracy possible. The vector of function values is near zero, as measured by the value of the function tolerance. However, the last step was ineffective.
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 4], x0);
plot_results(t, Y, 'g', 'Pr = 10');
Pr = 0.01;
a = fsolve(@small_Pr, [0.15 -0.17], options);
No solution found. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance squared, but the vector of function values is not near zero as measured by the value of the function tolerance.
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 4], x0);
plot_results(t, Y, 'y', 'Pr = 0.01');
% Functions
function f = big_Pr(a)
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 7], x0);
f(1) = Y(end, 2);
f(2) = Y(end, 4)^2 + Y(end, 5)^2;
end
function f = small_Pr(a)
x0 = [0 0 a(1) 1 a(2)];
[t, Y] = ode15s(@governing_equation, [0 30], x0);
f(1) = Y(end, 2);
f(2) = Y(end, 4)^2 + Y(end, 5)^2;
end
function xdot = governing_equation(t, x)
global Pr
xdot(1) = x(2);
xdot(2) = x(3);
xdot(3) = -(1/Pr)*(0.5*x(2)^2 - (3*x(1)*x(3))/4) + x(4);
xdot(4) = x(5);
xdot(5) = 3*x(1)*x(5)/4;
xdot = xdot';
end
function plot_results(t, Y, color, label)
figure(1);
plot(t, -Y(:, 2), color);
hold on;
title('Vertical Velocity Profiles');
xlabel('η = (x/y) Ra_y^(^0^.^2^5^)');
ylabel('G = v(y/α) Ra_y^(^0^.^2^5^)');
legend(label);
figure(2);
plot(t, Y(:, 4), color);
hold on;
title('Temperature Profiles');
xlabel('η = (x/y) Ra_y^(^0^.^2^5^)');
ylabel('ϴ');
legend(label);
end
  • For Cases Where a Solution is Found: The solutions seem fine, but you should still verify them against expected results or additional criteria to ensure they are accurate and meaningful.
  • For the Case with Inaccuracy Possible: You might want to adjust the solver options like increasing the maximum number of iterations (MaxIter) or changing the function tolerance (TolFun). This could help the solver take more steps to find a better solution.
  • For the No Solution Found Case: This is a bit trickier. You might need to provide a better initial guess or adjust solver settings. In some cases, the problem formulation itself might need to be revisited, especially if it's a particularly challenging nonlinear problem.
Remember, solving nonlinear equations, especially those from complex systems, can be quite sensitive to initial conditions and solver settings. Fine-tuning these parameters is often a key part of the process.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Engines & Motors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by