Math Program / How to input a function ?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
wilou
Newbie
Posts: 6
Joined: Mon Apr 18, 2016 8:50 am

Math Program / How to input a function ?

Post by wilou »

Hello,

I'm new to this forum. I've used BASIC and QBASIC long time ago, when I was in college. I've discovered a floppy disk with programs I wrote years ago, and I want to improve them a little. Most of these programs are math ones. I've always been a big fan of Pocket Computers, back in the 90s. I still like them today, and I recently bought, among others, a Casio fx-850p with its RP33 memory extension (with its 40 kb of memory, I'm the King of the World ! :) ). It was the 1st time I was using Casio BASIC, and I like it very much. I've downloaded QB64 on my PC, and I see there are some differences between the two languages. And that's why I'm here.

I've numerous math programs, and today, I'm working on a one that allows me to draw functions. It's simple and runs quite nice. The program asks me minimum and maximum values of the x and y axis, then draws the function I've stored in a routine. I'd like to be able to let the program ask me to input the function. For now, I must edit the program to type the function I want to work with. On my Casio, there is a function name VALF which is interesting. I store the function in an alphanumeric variable (for exemple F$), and VALF calculate its numeric value and store it in a numeric variable (for example Z : Z=VALF(f$)). Here is the code I have for an integration program :

Code: Select all

10 CLS:PRINT "Calcul d'aires - Méthode des trapèzes :"
20 INPUT "Fonction f(x) "; F$
30 INPUT "a "; A : INPUT "b "; B
40 INPUT "Nombre d'iterations "; N
50 H=(B-A)/N
60 X=A : Z1=VALF(F$) : X=B : Z2=VALF(F$) : S=.5*(Z1+Z2)
70 X=A
80 FOR I=1 TO N-1
90 X=X+H
100 Z=VALF(F$) : S=S+Z
110 NEXT I
120 S=S*H
130 PRINT CHR$(129); "f(x) dx ="; S
140 END
And now, here is the code of the drawing program I'm working with today (in QB64) :

Code: Select all

'Draw Function
1
SCREEN 12
COLOR 11
CLEAR
CLS

DEFDBL A-Z

PRINT
PRINT TAB(20); " Tracé d'une fonction"
PRINT

PRINT " Echelle :"
INPUT " Valeur minimale de x "; xmin
INPUT " Valeur maximale de x "; xmax
INPUT " Valeur minimale de y "; ymin
INPUT " Valeur maximale de y "; ymax

CLS

WINDOW (xmin + xmin / 10, ymin + ymin / 10)-(xmax + xmax / 10, ymax + ymax / 10)
LINE (xmin, 0)-(xmax, 0)
LINE (0, ymin)-(0, ymax)

FOR i = xmin TO xmax STEP 1
    PSET (i, .125)
NEXT i
FOR j = ymin TO ymax STEP 1
    PSET (.125, j)
NEXT j

FOR x = xmin TO xmax STEP 1 / 128
    IF x = xmin THEN PSET (x, f(x)), 14
    LINE -(x, f(x)), 14
NEXT x

LOCATE 28, 1
INPUT " Encore (o/n) "; z$
IF z$ = "o" THEN 1 ELSE END

FUNCTION f (x)
f = x ^ 3 - 3 * x ^ 2 + 2
END FUNCTION
English is not my native language, I hope you have understood what I want to know. In a few words, I want an equivalent to the VALF function of my Casio in the QB64 i use on my computer.

Thank you very much ! :)

W.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Math Program / How to input a function ?

Post by burger2227 »

VALF = VAL in QB64 to convert strings to numbers. When VAL encounters letters it stops converting.

Code: Select all

10 CLS: PRINT "Calcul d'aires - Méthode des trapèzes :"
20 INPUT "Fonction f(x) "; F$
30 INPUT "a "; A: INPUT "b "; B
40 INPUT "Nombre d'iterations "; N
50 H = (B - A) / N
60 X = A: Z1 = VAL(F$): X = B: Z2 = VAL(F$): S = .5 * (Z1 + Z2)
70 X = A
80 FOR I = 1 TO N - 1
    90 X = X + H
    100 Z = VAL(F$): S = S + Z
110 NEXT I
120 S = S * H
130 PRINT CHR$(129); "f(x) dx ="; S
140 END
LINE and PSET need a color attribute value to be seen. Check the QB64 WIKI for WINDOW statement differences:

Code: Select all

'Draw Function
1
SCREEN 12
COLOR 11
CLEAR
CLS

DEFDBL A-Z

PRINT
PRINT TAB(20); " Tracé d'une fonction"
PRINT

PRINT " Echelle :"
INPUT " Valeur minimale de x "; xmin
INPUT " Valeur maximale de x "; xmax
INPUT " Valeur minimale de y "; ymin
INPUT " Valeur maximale de y "; ymax

CLS

'WINDOW (xmin + xmin / 10, ymin + ymin / 10)-(xmax + xmax / 10, ymax + ymax / 10)
LINE (xmin, 10)-(xmax, 10), 12
LINE (10, ymin)-(10, ymax), 10

FOR i = xmin TO xmax STEP 1
    PSET (i, .125), 13
NEXT i
FOR j = ymin TO ymax STEP 1
    PSET (.125, j), 14
NEXT j

FOR x = xmin TO xmax STEP 1 / 128
    IF x = xmin THEN PSET (x, f(x)), 14
    LINE -(x, f(x)), 14
NEXT x

LOCATE 28, 1
INPUT " Encore (o/n) "; z$
IF z$ = "o" THEN 1 ELSE END

FUNCTION f (x)
f = x ^ 3 - 3 * x ^ 2 + 2
END FUNCTION
Apostrophe ' can be used to comment out code like I did the WINDOW code.

http://www.qb64.net/wiki/index.php/Keyw ... phabetical
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
wilou
Newbie
Posts: 6
Joined: Mon Apr 18, 2016 8:50 am

Re: Math Program / How to input a function ?

Post by wilou »

Hello,

Thank you for your answer.

I tried the first code in QB64, but as I suspected, that doesn't work.
I tought that VAL could replace VALF, but those are two different functions.

If I look here : http://www.qb64.net/wiki/index.php/VAL (I had already read this page before posting), it's written that :
If the first string character is not a number VAL returns 0.
Therefore, if I use a mathematical function with the "x" character (and it can be the first character of my function), VAL will return 0.
A contrario, VALF recognize the character and can assign a numerical value to it.

Example :
f(x) =x^2-3
I want to calculate the area between 1 and 3.
So a = 1 and b = 3. I will use n=50.
In my program, when the calculation begins, H=(B-A)/N, so H=(3-1)/50.
Then x=a (this is interesting, because I affect the value of a to x, to calculate f(x) = x^2-3 where x = a. If I replace x by a, I will have Z1 = (1^2)-3, or Z1 = -2. While VAL (F$) will consider the first character of F$ is a x so x = 0. The result is not what we expect... :(

I've already tried with VAL, it gives nothing right in this case...

On the other hand, I've no problem with WINDOW and PSET, but I'll give a try with your modifications, to see what happens.

Thanks ! :)

W.

EDIT : In fact, the description of VAL itself, on the wiki, says us it's not the thing we want :
The VAL Function returns the decimal numerical equivalent value of a STRING numerical value.
Numerical, that's the problem. VAL doesn't do the job with letters.

EDIT (again) : Without WINDOW, my second program doesn't work. Here is what I get with your code :
Image
With my code, everything is fine :
Image
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Math Program / How to input a function ?

Post by burger2227 »

There is no equivalent function in QB64. You could scan the text with your own function.

Try MID$, LEFT$ or RIGHT$ to read each string character in a FOR loop.

I do not have time now to address your other problems. Join QB64 Forum link below.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply