I can't take several non-linear inputs and assign them names for using later in the code.

1 view (last 30 days)
I am trying to do the following operation:
a=input('Enter the no. of equations');
counter=1;
while counter<=a
f_counter=input('enter the equation','s');
F(counter)=f_counter;
counter=counter+1;
end
F is a row vector ,supposed to be,changing size and storing the non-linear equations entered by the user. The problem is 'f_counter' is being considered as a single name instead of f_1 , f_2 etc. I am writing a code for Newton's method for non-linear equations(if that helps).

Accepted Answer

Matt J
Matt J on 5 Nov 2014
Edited: Matt J on 5 Nov 2014
The problem is 'f_counter' is being considered as a single name instead of f_1 , f_2 etc.
Even if 'f_counter' did what you were hoping, it would (a) be bad coding practice and (b) serve no purpose because you are putting the input in F anyway. There's no reason the inputs have to be held in 2 places, as far as I can see. However, F needs to be a cell array as below
a=input('Enter the no. of equations');
counter=1;
while counter<=a
F{counter}=input('enter the equation','s');
counter=counter+1;
end
  7 Comments
Matt J
Matt J on 6 Nov 2014
Edited: Matt J on 6 Nov 2014
Well, for a full understanding, you'll probably need to read up on cell array manipulation,
However, you can also gain an understanding of what my commands are doing by running them individually at the command line and displaying their output. For example, below you can see that F(:,2)={'; '} is just adding a column of semicolons to the original F,
>> F={'x+y';'x*y'}, F(:,2)={'; '},
F =
'x+y'
'x*y'
F =
'x+y' '; '
'x*y' '; '
Ahsun Ali
Ahsun Ali on 6 Nov 2014
I am grateful to you for your great help Matt J. I appreciate your assistance every step of the way. Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!