Error while contour plotting an integral.
3 views (last 30 days)
Show older comments
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 Comments
Star Strider
on 20 Aug 2023
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.
Answers (1)
Pooja Kumari
on 29 Aug 2023
Edited: Pooja Kumari
on 29 Aug 2023
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:
- Convert the variables to symbolic scalar variables using “syms” function in MATLAB. You can refer to the below documentation for more information: Create symbolic scalar variables and functions, and matrix variables and functions - MATLAB syms - MathWorks India
Here is a sample code for reference:
syms x z t x1 z1 t1;
- “matlabFunction” can be used to convert symbolic function to numeric function handle specifying the “Vars” as [x,z,t,x1,z1,t1]. You can refer to the below documentation for more information on matlabFunction: Convert symbolic expression to function handle or file - MATLAB matlabFunction - MathWorks India
- In the numerical integration using “integral” function, you can use “t1” which will resolve the “Warning: Inf or NaN value encountered.”
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
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!