Cant assign an input in a function with an elseif condition

Hi, I am trying to form a simple function which the user has to make two branching choice. The function receives an initial input of a time step size, and it should be used in both if scenarios. However, it only works for the first case but not the second. I attached the two .m files and this is the code:
function runselected()
%clear I have deleted these two after the comments.
%clc
time = input('Enter a time step value for the question which should be less than or equal to 10e-03 to overcome convergence problems: ')
dt= time;
choice = input('Enter 1 to run the solution for question a, 2 for b: ');
if choice == 1
fprintf('Running for a: \n');
run("hw4_a.m")
T_FTCS
T_CN
elseif choice == 2
time = input('Enter a time step value for the question (b) which should be less than or equal to 10e-03 to overcome convergence problems: ');
dt= time;
fprintf('Running for b: \n');
run("hw4_b.m")
else
fprintf('Invalid choice. Enter either "1" or "2". \n');
end
end
% This is the running function
%This is the error I get:
%Unrecognized function or variable 'dt'.
%Error in hw4_b (line 31)
% T_FTCS_new(i, j) = T_FTCS(i, j) + 1/alpha * dt * ((T_FTCS(i+1, j) - 2*T_FTCS(i, j) + T_FTCS(i-1, j)) / dx^2 + (T_FTCS(i, j+1) - 2*T_FTCS(i, j) + T_FTCS(i, j-1)) / dy^2);
%Error in run (line 91)
%evalin('caller', strcat(script, ';'));
%Error in runselected (line 20)
% run("hw4_b.m")

4 Kommentare

Dyuman Joshi
Dyuman Joshi am 27 Dez. 2023
Verschoben: Dyuman Joshi am 27 Dez. 2023
The line where dt is defined in the script hw4_b is commented out, thus the variable is not defined, which when used to define other variables gives the error.
Solution - Uncomment it.
Also, you don't need to use clear and clc under a function, so you can remove them.
Yes, but that's the gist of it. Neither the hw4_a has a defined dt value, it is also commented out. I want to define it through the
time = input('Enter a time step value for the question which should be less than or equal to 10e-03 to overcome convergence problems: ')
dt= time;
lines. It works for the hw4_a but not the hw4_b, in otherwords the elseif command.
I'm not sure what's the anomaly here, but if you want to pass a value, define the files as functions.
I have changed the names of the function files, commented out input() calls, as they don't work here and converted choice as input to the main function just to show that both the choices work properly -
%choice = 1
runselected(1)
Running for a:
%choice = 2
runselected(2)
Running for b: FTCS: Temperature at middle point is 15°C at t = 6.445 seconds. Crank-Nicholson: Temperature at middle point is 15°C at t = 5.96 seconds.
%Main Function
function runselected(choice)
%input('Enter 1 to run the solution for question a, 2 for b: ');
if choice == 1
dt = 0.005; %input('Enter a time step value for the question which should be less than or equal to 10e-03 to overcome convergence problems: ');
fprintf('Running for a: \n');
fun_hw4_a(dt)
elseif choice == 2
fprintf('Running for b: \n');
dt = 0.005; %input('Enter a time step value for the question (b) which should be less than or equal to 10e-03 to overcome convergence problems: ');
fun_hw4_b(dt)
else
fprintf('Invalid choice. Enter either "1" or "2". \n');
end
end
Automatically writing CLEAR, CLOSE, CLC at the top of every piece of code:

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 27 Dez. 2023
Bearbeitet: Cris LaPierre am 27 Dez. 2023

0 Stimmen

Remove the clear statements in your code.

3 Kommentare

I did, but still the same error pops up.
Did you remove the clear that is at the top of your hw4_b.m script as well?
Can't believe I missed it... Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by