Question: I keep getting the following error code in my MATLAB code, and I can not figure out how to fix it . Below is the
I keep getting the following error code in my MATLAB code, and I can not figure out how to fix it Below is the error code I keep receiving, followed by my MATLAB code. What do I need to change in my code to correct the problem?
Error using fzero line
Initial function value must be finite and real.
Error in main line
z fzero@z shootingmethodx z L sigmaprime, Tinf, T TL zguess;
MATLAB CODE:
function main
L ; Length of the rod m
N ; Number of spatial grid points
Constants
sigmaprime e; K m
Tinf ; K
Boundary conditions
T; K at x
TL ; K at xL
Spatial grid
x linspace L N; Including boundary points
Shooting Method
zguess ; Initial guess for z
z fzero@z shootingmethodx z L sigmaprime, Tinf, T TL zguess;
~ Tshooting shootingmethodx z L sigmaprime, Tinf, T TL;
Finite Difference Method
Tfd finitedifferencemethodL N sigmaprime, Tinf, T TL;
Plotting
figure;
plotx Tshooting, b 'LineWidth', ;
hold on;
plotx Tfdr 'LineWidth', ;
hold off;
xlabelx m;
ylabelTemperature K;
titleTemperature Distribution in a Rod';
legendShooting Method', 'Finite Difference Method';
grid on;
end
function residual T shootingmethodx z L sigmaprime, Tinf, T TL
Shooting Method Solver
Number of spatial grid points
N lengthx;
Spatial step size
dx L N ;
Initialize y and z vectors
y zerosN;
z zerosN;
Set initial conditions
y T;
z z;
Numerical integration using Euler's method
for i :N
zi zi dx sigmaprime Tinf yi;
yi yi dx zi;
end
Calculate the residual at the right boundary xL
residual yend TL;
T y;
end
function T finitedifferencemethodL N sigmaprime, Tinf, T TL
Finite Difference Method Solver
Spatial grid
x linspace L N; Including boundary points
Spatial step size
dx L N ;
Coefficient matrix for finite difference equation
A diagonesN diagonesN diagonesN;
A A dx;
Righthand side vector
b sigmaprime Tinf T onesN;
b b Tdx;
bend bend TLdx;
Solve the system of equations
Tinterior Ab;
Concatenate the boundary conditions to get the final temperature profile
T T; Tinterior; TL;
end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
