HP Forums
Issue with CAS solver function involving passing strings to the variable position. - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Issue with CAS solver function involving passing strings to the variable position. (/thread-15666.html)



Issue with CAS solver function involving passing strings to the variable position. - Grayhek - 09-30-2020 04:06 PM

I am writing a program that asks what the user would like to solve for and then pass that into the CAS Solve function. The program look like this:



[code]//Amplitude
export Amplitude()
BEGIN
Local Find,Find_1,variables,Answer;
Local Â,K,τ,ω;

print();
//K=5, Â=0.0355 τ=10 ,ω=3.1415 ,E=.5 ,A=7
A:=0;

CHOOSE(A,"Which Amplitude?","Final","Initial");
if A==1 then
variables:={"Â","K","τ","ω","E","A"};
input({{Find,variables,{56,15,0}},{Â,[0],{10,20,1}},{K,[0],{10,20,2}},{τ,[0],{10,20,3}},{ω,[0],{10,20,4}},{E,[0],{10,20,5}},{A,[0],{10,20,6}}});
Find_1:=variables[Find]; Find:=Find_1;
Answer:=CAS("SOLVE(Â=K*A/(sqrt((1-(τ*ω)^2)^2+(2*τ*ω*E)^2)),Find,1)");
print(Answer);
end;
END;[code]

When I run this it returns "Error: Constant function"
I have tried expr(Find) and also EVAL(Find) but both options failed to work.


RE: Issue with CAS solver function involving passing strings to the variable position. - Gene222 - 10-01-2020 10:07 AM

I'm just a novice, but it looks like you mixing text and variables in the string expression. Try something like:
Answer:=CAS("SOLVE(Â=K*A/(sqrt((1-(τ*ω)^2)^2+(2*τ*ω*E)^2))," + Find + ",1)");


RE:Issue with CAS solver function involving passing strings to the variable position. - Grayhek - 10-01-2020 12:10 PM

(10-01-2020 10:07 AM)Gene222 Wrote:  I'm just a novice, but it looks like you mixing text and variables in the string expression. Try something like:
Answer:=CAS("SOLVE(Â=K*A/(sqrt((1-(τ*ω)^2)^2+(2*τ*ω*E)^2))," + Find + ",1)");

So it works better than before in that it doesn't return an error but it doesn't evaluate the equation. I normally ran into that problem when I didn't us the program in the solver app but I checked and made sure I was in the Solver app and it still was not running properly. I ran it in the CAS environment and it returned an error.
Code:

//Amplitude 
        export Sys_Ctrl_Amplitude()
        BEGIN
            Local Find,Find_1,variables,Answer;
            print();
            
            G:=0;
            CHOOSE(G,"Which Amplitude?","Final","Initial");
            if G==1 then
                variables:={"Â","K","τ","ω","ζ","A"};
                input({{Find,variables,{56,15,0}},{Â,[0],{10,20,1}},{K,[0],{10,20,2}},{τ,[0],{10,20,3}},{ω,[0],{10,20,4}},{ζ,[0],{10,20,5}},{A,[0],{10,20,6}}});
                Find_1:=variables[Find]; Find:=Find_1;
                //input({{K,[0],{10,20,2}},{τ,[0],{10,20,3}},{ω,[0],{10,20,4}},{ζ,[0],{10,20,5}},{A,[0],{10,20,6}}});
                Answer:=CAS("SOLVE(Â=K*A/(sqrt((1-(τ*ω)^2)^2+(2*τ*ω*ζ)^2)),"+Find+",1)");
                //print("Â= "+Answer);
                print(Find+"= "+Answer);
            end;
        END;



RE: Issue with CAS solver function involving passingstrings to the variable position. - Gene222 - 10-01-2020 04:38 PM

I don't know how to get SOLVE or fsolve to solve your equation. Some comments on your first program are:
(1) Your choose variable A is the same as your variables[6] which is also defined as A.
(2) I tried using fsolve instead of SOLVE, but it gave {} which I think means fsolve could not find an answer for the initial guess.
(3) I tried exporting the solve variables, but that did not help.
Below are the changes I made to your program in trying to get the program to work. I predefined your list variables, because I got tired of inputting the values in the INPUT screen.
Sorry I could not help you.
PHP Code:
EXPORT Â,τ,ω,A1,Answer;
EXPORT Find,Find_1,variables;

export Amplitude()
BEGIN
print();

Â:=0.0355;
K:=5;
τ:=10;
ω:=3.1415;
E:=0.5;
A1:=7;


CHOOSE(A,"Which Amplitude?","Final","Initial");
if 
A==1 then
variables
:={"Â","K","τ","ω","E","A1"};
input({{Find,variables,{56,15,0}},{Â,[0],{10,20,1}},{K,[0],{10,20,2}},{τ,[0],{10,20,3}},{ω,[0],{10,20,4}},{E,[0],{10,20,5}},{A1,[0],{10,20,6}}});
Find_1:=variables[Find]; Find:=Find_1;
Answer:=fsolve("Â=K*A/(sqrt((1-(τ*ω)^2)^2+(2*τ*ω*E)^2)),"+Find+"=1)");
print(
Answer);
end;
END



RE: Issue with CAS solver function involving passing strings to the variable position. - Grayhek - 10-01-2020 09:55 PM

yes I tried that as well period on my calculator I already had them as global variables in another program labeled load_variables (). I just put them in as local variables so that people would not get lost reading the program. I may have to resort to waiting multiple If statements/Cases or figure out how to utilize the solver app. I'm reluctant to do this
1) because I don't know how to program utilizing an app
2) because multiple If statements would clutter up the code.


RE: Issue with CAS solver function involving passing strings to the variable position - Gene222 - 10-01-2020 10:43 PM

You might have a problem with the names you used for your variables. See the following posts below.
HP Prime Solver Variables Issue
Programming solve function

I also have had problems with expressions that included trig functions that gave multiple answers, where I had to either use an initial guess that would narrow the search for a solution, or put a limit on the solve variable such as 0.0001..2*pi.

I also had problems where the variables would give a division by zero error or where the change in the parameter variables would not significantly change the solve variable, so that fsolve could not find a solution. In one case, I ended up replacing fsolve with a trial and error/bisection method in order to find a solution.