Outputting arrays from function in for loop

7 views (last 30 days)
Hello, I am not really sure the best way to describe this problem... Essentially what I want to do is solve a function that outputs an array, while in a for loop. If you look at the code, lines 45 through 48 are the parts in question. Rather than solving dT three separate times, I would like to solve it within a for loop that will output something like T(i), then call an entire column and plot it.
clc;
clear all;
close all;
%%Solution Outline
%%Assumptions
%%Paranmeters
% nio for niobium
nio.d = 0.01; % [m] Diameter of the sphere
nio.Ti = 900 + 273; % [K] Initial Temperature
nio.Tf = 300 + 273; % [K] Final Temperature
nio.rho = 8600; % [kg/m^3] Density
nio.c = 290; % [J/kg K] Specific Heat
nio.k = 63; % [W/m K] Thermal Conductivity
nio.Tinf = 25 + 273; % [K] Assumed Room Temperature
nio.A = 4*pi*(nio.d/2)^2; % [m^2] Surface Area of the Sphere
nio.V = 4/3*pi*(nio.d/2)^3;%[m^3] Volume of Sphere
nio.eps = 0.6; % Emissivity
h = [10,200,500]; % [W/m^2 K] Convection Coefficient
sigma = 5.67e-8; % Stefan Boltzmann constant
%%Calculations
% The first step is to create a plot with the various h values
% How can I do the following in a for loop?
[t1,T1] = dT(nio,h(1));
[t2,T2] = dT(nio,h(2));
[t3,T3] = dT(nio,h(3));
plot(t1,T1,t2,T2,t3,T3);
%%Sub Function
function [t,T] = dT (nio,h)
dT = @(t,T) ( -h*nio.A*(T-nio.Tinf) - nio.A*sigma*nio.eps*(T^4 - ...
nio.Tinf^4)) / (nio.rho*nio.V*nio.c);
[t,T] = ode45 (dT, [0,60] , [nio.Ti]);
end
end

Accepted Answer

OCDER
OCDER on 29 Sep 2017
Edited: OCDER on 29 Sep 2017
Going to use nio.sigma instead of sigma, since sigma is a matlab function and NOT a constant that you want in your function dT.
nio.sigma = 5.67e-8; % Stefan Boltzmann constant
To fix the plotting, do something like this:
Data = cell(2, 3); %row1 = t1,t2,t3, row2 = T1,T2,T3
for j = 1:3
[Data{1, j}, Data{2, j}] = dT(nio,h(j));
end
plot(Data{:})
Found some issues with your function, see comments. Make sure this is the same equation though!
function [t,T] = dT(nio,h)
%RECHECK CHANGES to make they are the same eqn!
%dT = @(t,T) ( -h*nio.A*(T-nio.Tinf) - nio.A*sigma*nio.eps*(T^4 - nio.Tinf^4)) / (nio.rho*nio.V*nio.c);
%- dont' name dT function handle same as your dT function name. It gets confusing.
% > using aT instead
%- sigma is matlab function, so you can't use that as a variable.
% > using nio.sigma instead
aT = @(t,T) ( -h*nio.A.*(T-nio.Tinf) - nio.A*nio.sigma*nio.eps*(T.^4 - nio.Tinf^4)) / (nio.rho*nio.V*nio.c);
[t, T] = ode45(aT, [0,60] , [nio.Ti]);

More Answers (0)

Categories

Find more on Programming 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!