필터 지우기
필터 지우기

I want to design a Homework Problem Solver

조회 수: 4 (최근 30일)
Evan Jones
Evan Jones 2022년 2월 1일
답변: ali hassan 2022년 2월 2일
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
ali hassan
ali hassan 2022년 2월 2일
P=100,V=2,T=273,R=8.314
you need to write your question script in above form. only then you can easily play with it. otherwise, in my humble knowledge, MATLAB cannot extract variables from the string especially in your case,when the units are mentioned as well.
Rik
Rik 2022년 2월 2일
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'}

댓글을 달려면 로그인하십시오.

답변 (2개)

DGM
DGM 2022년 2월 2일
편집: DGM 2022년 2월 2일
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
ali hassan
ali hassan 2022년 2월 2일
this is the solution which i can suggest in my humble knowlegde,experts can improve.
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
DGM
DGM 2022년 2월 2일
You can post it as an answer if you want to get credit for it.

댓글을 달려면 로그인하십시오.


ali hassan
ali hassan 2022년 2월 2일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by