MATLAB Answers

Matlab Homework that im struggling with

2 views (last 30 days)
Jacob Brackett
Jacob Brackett on 4 Mar 2020
Commented: Jacob Brackett on 5 Mar 2020
OK, this is my homework asignment that I am working on and having trouble getting started. I believe I can write code to solve using cramers rule however, I dont know how to prompt the user to enter the number of equations/unknowns, between 2 and 5. Then Im not sure how to prompt the user again for them to enter the constants and coefficents for each equation. If yall could give me any insight on how to do this I would be very grateful.

  0 Comments

Sign in to comment.

Answers (1)

Jim Riggs
Jim Riggs on 4 Mar 2020
To prompt a user to enter a value, try:
x = input('Enter the number of equations (from 2 to 5): \n')

  4 Comments

Show 1 older comment
Jim Riggs
Jim Riggs on 5 Mar 2020
neq = input('Enter the number of equations (from 2 to 5): \n') % number of equations
c = nan(neq,1); % create vector c for the coefficients
for n=1:neq
msg = sprintf('Enter coefficient %i: ',n); % create a prompt string
c(n) = input([msg '\n']); % prompt the user and store the value in c
end
The '\n' inserts a "new line" character, and the square brackets concatenates the msg string with the new line character. This causes the input prompt to be on the line following the text prompt.
If you dont like this format, you can leave out the new line character. Try it like this:
neq = input('Enter the number of equations (from 2 to 5): ') % number of equations
c = nan(neq,1); % create vector c for the coefficients
for n=1:neq
msg = sprintf('Enter coefficient %i: ',n); % create a prompt string
c(n) = input(msg); % prompt the user for the coefficients and store the value in c
end
John D'Errico
John D'Errico on 5 Mar 2020
@Jacob - If you will allow input like that, then you will need to write custom code that can parse it. I will suggest that task is well beyond your current capabilities, if you don't even know how to use input. Why? Because you will need to write code that can find terms like 4x, or perhaps 3.14y, and recognize there is a missing multiplication in there. Worse, how about an expression like this: 2x + 1e4y = 1.2e-3.
I would strongly suggest you limit your problem, to be able to handle the case where the equations are entered in a simple way as just a numeric vector of coefficients. Learn to walk before you try to run.
Jacob Brackett
Jacob Brackett on 5 Mar 2020
Ok, @John D'Errico how would you suggest doing it. This is my first semester of coding and I'm still learning the basics.

Sign in to comment.


Translated by