I want to design a Homework Problem Solver

1 view (last 30 days)
I'm looking for help in making a program that would allow me to input given values from a problem in a script, and output all of the potential solutions, given the givens. For instance:
"Given P = 100 kpa, V = 2 litres, T = 273 K, R = 8.314 Solve for n."
The script would search through another script that would have all of the functions with all of the equations for the given class, and then output the variable that was not given. So if you had these two equations:
PV = nRT
E = mc^2
It would be able to tell that the first equation has all of the variables except for n, and would therefore output the value for n.
I am looking to do a significant amount of work on this project, as it will likely save me a lot of time and I kind of think it would just be a cool project. I am not looking for a step-by-step on how to do this (unless you really want to). I am just looking for some help in finding what to google to get started. I'm thinking I may need to do some sort of function classdef thing, but I don't know how to do the selection for what equation to use based off of what variables are given.
Any help would be greatly appreciated
TL;DR: Homework hard, need help procrastinating via matlab
  3 Comments
Rik
Rik on 2 Feb 2022
Having the units in there is not even the problem. The problem is parsing the rest of the sentence. One of the problems is also the missing unit for R.
You could probably solve this with a regular expression by selecting the characters before an equal sign:
str='Given P = 100 kpa, V = 2 litres, T = 273 K, R = 8.314 Solve for n.';
x=regexp(str,'\s(\S*)\s*=\s*','tokens');
x=[x{:}]
x = 1×4 cell array
{'P'} {'V'} {'T'} {'R'}

Sign in to comment.

Answers (2)

DGM
DGM on 2 Feb 2022
Edited: DGM on 2 Feb 2022
Here's part of a solution:
instr = 'Given P = 100 kpa, V = 2 litres, T = 273 K, R = 8.314 Solve for n.';
P = str2double(regexp(instr,'(?<=\s+P\s*=\s*)\d*\.*\d*(?=\D)','match'))
P = 100
V = str2double(regexp(instr,'(?<=\s+V\s*=\s*)\d*\.*\d*(?=\D)','match'))
V = 2
T = str2double(regexp(instr,'(?<=\s+T\s*=\s*)\d*\.*\d*(?=\D)','match'))
T = 273
R = str2double(regexp(instr,'(?<=\s+R\s*=\s*)\d*\.*\d*(?=\D)','match'))
R = 8.3140
n = str2double(regexp(instr,'(?<=\s+n\s*=\s*)\d*\.*\d*(?=\D)','match'))
n = []
Which I'm sure can be improved or done otherwise.
EDIT: For "improved", see Rik's comment
x = regexp(instr,'\s(\S*)\s*=\s*(\d*\.*\d*)','tokens');
x = vertcat(x{:})
x = 4×2 cell array
{'P'} {'100' } {'V'} {'2' } {'T'} {'273' } {'R'} {'8.314'}
This assumes you already know what variables you're looking for and how they will be presented (formatting, units, case). If you're asking to be able to parse any text and create a bunch of unforeseen named variables, that's a bad idea.
If you know some equation needs P, V, n, R, and T, then you can look for each one. If one is empty, you can use that information. Being able to adapt to different units is an extra complication.
  5 Comments
DGM
DGM on 2 Feb 2022
You can post it as an answer if you want to get credit for it.

Sign in to comment.


ali hassan
ali hassan on 2 Feb 2022
there are two possible answers for it:
1) exracting the variables from string. a solution suggested by @DGM @Rik. i just put loops in it.
2) changing the question format and make it easy.
Solution#01
instr = 'Given P = 100 kpa, V = 2 litres, T = 273 K, R = 8.314 Solve for n.';
P = str2double(regexp(instr,'(?<=\s+P\s*=\s*)\d*\.*\d*(?=\D)','match'))
V = str2double(regexp(instr,'(?<=\s+V\s*=\s*)\d*\.*\d*(?=\D)','match'))
T = str2double(regexp(instr,'(?<=\s+T\s*=\s*)\d*\.*\d*(?=\D)','match'))
R = str2double(regexp(instr,'(?<=\s+R\s*=\s*)\d*\.*\d*(?=\D)','match'))
n = str2double(regexp(instr,'(?<=\s+n\s*=\s*)\d*\.*\d*(?=\D)','match'))
if isempty(P)
P=(n*R*T)/V
end
if isempty(V)
V=(n*R*T)/P
end
if isempty(n)
n=(P*V)/(R*T)
end
if isempty(R)
R=(P*V)/(n*T)
end
if isempty(T)
T=(P*V)/(n*R)
end
Solution no 2
P=100,V=2,T=273,R=8.314; %question
if exist('P')==0 %checking existance of P variable.
P=(n*R*T)/V
end
if exist('V')==0 %checking existance of V variable.
V=(n*R*T)/P
end
if exist('n')==0 %checking existance of n variable.
n=(P*V)/(R*T)
end
if exist('R')==0 %checking existance of R variable.
R=(P*V)/(n*T)
end
if exist('T')==0
T=(P*V)/(n*R) %checking existance of T variable.
end

Community Treasure Hunt

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

Start Hunting!