Post Reply 
Issue with CAS solver function involving passing strings to the variable position.
09-30-2020, 04:06 PM
Post: #1
Issue with CAS solver function involving passing strings to the variable position.
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.
Find all posts by this user
Quote this message in a reply
10-01-2020, 10:07 AM
Post: #2
RE: Issue with CAS solver function involving passing strings to the variable position.
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)");
Find all posts by this user
Quote this message in a reply
10-01-2020, 12:10 PM (This post was last modified: 10-01-2020 12:21 PM by Grayhek.)
Post: #3
RE:Issue with CAS solver function involving passing strings to the variable position.
(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;


Attached File(s) Thumbnail(s)
       
Find all posts by this user
Quote this message in a reply
10-01-2020, 04:38 PM (This post was last modified: 10-01-2020 04:47 PM by Gene222.)
Post: #4
RE: Issue with CAS solver function involving passingstrings to the variable position.
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
Find all posts by this user
Quote this message in a reply
10-01-2020, 09:55 PM
Post: #5
RE: Issue with CAS solver function involving passing strings to the variable position.
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.
Find all posts by this user
Quote this message in a reply
10-01-2020, 10:43 PM (This post was last modified: 10-02-2020 05:57 PM by Gene222.)
Post: #6
RE: Issue with CAS solver function involving passing strings to the variable position
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.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)