Error while contour plotting an integral.

I am trying to plot this temperature equation like a simulation. The temperature should be a function of x and z with t as varying time step. We are getting this error: Input arguments must be numeric or objects which can be converted to double.
I have written the following code. Can someone please help in this?
% Material Properties
P= 50;
eta= 0.3;
rho=4429;
D=2.89*10^(-6);
Cp=553;
T0=300;
v=0.1;
%function% Define the range of x and z values
x_range = linspace(-10, 10, 100); % Adjust the range and number of points as needed
z_range = linspace(-10, 10, 100); % Adjust the range and number of points as needed
fun = exp(-((z - z1).^2 + (t/10 - t1/10 - x + x1).^2)./((6823819567746637*t)./590295810358705651712 - (6823819567746637*t1)./590295810358705651712))./(t - t1).^(3/2);
% Create a grid of x and z values
[X, Z] = meshgrid(x_range, z_range);
% Define the number of time steps
num_time_steps = 10; % Adjust as needed
% Initialize a cell array to store the integral results for each time step
integral_results = cell(num_time_steps, 1);
% Calculate the integral result for each time step
for t_step = 1:num_time_steps
t = t_step * 0.1; % Adjust the time step as needed
% Initialize a matrix for this time step
integral_result = zeros(length(x_range), length(z_range));
for x_index = 1:length(x_range)
for z_index = 1:length(z_range)
x = x_range(x_index);
z = z_range(z_index);
% Numerical integration using quad
fun_numeric = @(t1) double(subs(fun, {x, z, t, x1, z1}, {x, z, t, x, z}));
integral_result(x_index, z_index) = integral(fun_numeric, 0, t);
end
end
% Store the result in the cell array
integral_results{t_step} = integral_result;
end
% Create contour plots for each time step
for t_step = 1:num_time_steps
t = t_step * 0.1; % Adjust the time step as needed
figure;
contourf(X, Z, integral_results{t_step}, 20); % Adjust the number of contour levels as needed
colorbar;
xlabel('x');
ylabel('z');
title(['Contour Plot at t = ' num2str(t)]);
end

5 个评论

I do not understand what you want to do.
That aside, this runs, however you will likely need to make changes to it (specifically the arguments to ‘fun’) to get the desired result —
% Material Properties
P= 50;
eta= 0.3;
rho=4429;
D=2.89*10^(-6);
Cp=553;
T0=300;
v=0.1;
%function% Define the range of x and z values
x_range = linspace(-10, 10, 100); % Adjust the range and number of points as needed
z_range = linspace(-10, 10, 100); % Adjust the range and number of points as needed
fun = @(x,z,t,x1,z1,t1) exp(-((z - z1).^2 + (t/10 - t1/10 - x + x1).^2)./((6823819567746637*t)./590295810358705651712 - (6823819567746637*t1)./590295810358705651712))./(t - t1).^(3/2);
% Create a grid of x and z values
[X, Z] = meshgrid(x_range, z_range);
% Define the number of time steps
num_time_steps = 10; % Adjust as needed
% Initialize a cell array to store the integral results for each time step
integral_results = cell(num_time_steps, 1);
% Calculate the integral result for each time step
for t_step = 1:num_time_steps
t = t_step * 0.1; % Adjust the time step as needed
% Initialize a matrix for this time step
integral_result = zeros(length(x_range), length(z_range));
for x_index = 1:length(x_range)
for z_index = 1:length(z_range)
x = x_range(x_index);
z = z_range(z_index);
% Numerical integration using quad
% fun_numeric = fun(x,z,t,x,z)
% fun_numeric = @(t1) double(subs(fun, {x, z, t, x1, z1}, {x, z, t, x, z}));
integral_result(x_index, z_index) = integral(@(t)fun(x,z,t,x,z,t_step), 0, t);
end
end
% Store the result in the cell array
integral_results{t_step} = integral_result;
end
% Create contour plots for each time step
for t_step = 1:num_time_steps
t = t_step * 0.1; % Adjust the time step as needed
figure;
contourf(X, Z, integral_results{t_step}, 20); % Adjust the number of contour levels as needed
colorbar;
xlabel('x');
ylabel('z');
title(['Contour Plot at t = ' num2str(t)]);
end
I am not running the code here because it takes longer than the allotted 55 seconds. Running it would simply show that it timed out.
The ‘fun_numeric’ assignment is not required. Create ‘fun’ as a function that does what you want, provide the correct arguments to it, and the code should do what you want.
.
fun_numeric = @(t1) double(subs(fun, {x, z, t, x1, z1}, {x, z, t, x, z}));
Most of the time if you are wanting a numeric result from a symbolic calculation, you should use matlabFunction
You could probably do that outside some of the existing loops
Thanks for your answer @Star Strider. However, I am getting a warning as infinity or nan value encountered. I tried to solve this error by changing the limit of t, but it is not working. I just want to run a simulation of the temperature field as a function of x and z from the equation given. Can you please help in this matter?
Did you use my revision of your code, or something else?
If you changed it, please post the new code.
When I ran it, the code I posted runs without error, although it takes forever, so I stopped it before it finished.
Can you tell what is y and y' in the image attached above?

请先登录,再进行评论。

回答(1 个)

Dear AD,
I understand that you are facing issue with contour plot of this integral function provided in the temperature function below:
These are some troubleshooting steps which you can follow:
Here is a sample code for reference:
syms x z t x1 z1 t1;
Here is a sample code for reference:
q = integral(@(t1) fun(t1,5),0,2);
You can refer to the below documentation for more information on contour plot:
Regards,
Pooja Kumari

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

产品

版本

R2022b

提问:

AD
2023-8-19

Community Treasure Hunt

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

Start Hunting!

Translated by