• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Matrices, graph plotting, or symbolics in PHP?

Nick

Well-Known Member
NLC
I'm attempting to do some web-based calculations with various physics and engineering equations. I'm familiar with programming in C++ and Matlab, and I've tinkered around with Perl years ago but that was mostly just minimal modification other peoples' scripts.

I looked it up on the internet and found out that I do have access to hyperbolic, logarithmic, exponential, and other higher functions that I will need... The $5 PHP beginners book I found in the clearance box at the book store to get acquainted didn't mention any of these, go figure :p.

What I'm wondering is, how far does PHP's ability in math go? I was thinking about writing some scrips involving Jacobian and other similar functions. Can I create matrices and then multiply, divide, or take the transpose?

What about symbolic type stuff, similar to what you can do in Matlab with the symbolic toolkit? Are there functions that would allow me to integrate and take derivatives? Either to get a numerical value or maybe output a symbolic answer?

Last question refers to graph plotting. Can I do it? Is there any ability to have an image of a graph generated from a mathematical function or some data set?

If any/all of these are not available, is there by any chance some sort of extension I can add to PHP to allow further functionality?
 
You need GD Libary but I myself have never tried to make images in PHP. Never needed to really. Ask Joe, I think he knows how.
 
I have no idea when it comes to math, or physics, or complex graphs ... show me what you'd write in C++ and I'll tell you if it's possible in PHP ... go slowly I suck at math, don't assume I know what a math function does in C++ either ...

Sorry I cant be of more help ...
 
And also, reading through the Math extension on php.net brings up quite a few of the things you're looking for ...

http://php.net/manual/en/ref.math.php

Math is standard extension you should have it already ...

That covers all ( mathematics ) you need right ? If something isn't covered then show me in C++ or name some methods/classes ...

The easy bit for last, it's perfectly possible to generate an image from a dataset, however I would encourage you not to waste your time writing your own solution, rather just find one that suits your needs, try searching phpclasses.org or just google for "php graph" ...
 
Last edited:
Most of the stuff I mentioned is actually Matlab stuff. The graphs would be nice, but my main concern right now is really the matrices.

For example, say I had wanted to solve a series of equations that were setup like this:

(A1)(x1) + (A2)(x2) + (A3)(x3) = (K1)
(B1)(x1) + (B2)(x2) + (B3)(x3) = (K2)
(C1)(x1) + (C2)(x2) + (C3)(x3) = (K3)

I can solve them with matrices by setting up matrices like this:

Code:
[A1 A2 A3] [x1] [K1]
[B1 B2 B3]x[x2]=[K2]
[C1 C2 C3] [x3] [K3]

Where

    [A1 A2 A3]
A = [B1 B2 B3]
    [C1 C2 C3]

    [x1]
x = [x2]
    [x3]

    [K1]
b = [K2]
    [K3]

Well, in Matlab I can just do this:

Code:
% equations
% (k1+k2)x1 - k2x2 = g(m1)
% -k2x1 + (k2+k3)x2 - k3x3= g(m2)
% -k3x2 + k3x3 = g(m3)
% for this problem k1 = k2 = k3 = k

m1 = 2;
m2 = 3;
m3 = 2.5;

k = 10;
g = 9.81;


A = [2*k  -k  0;-k  2*k  -k; 0 -k k]

b = [g*m1; g*m2; g*m3]


x = A\b

% Alternatively, I can also do this:
% x = inv(A)*b

Is this kind of thing going to be possible?
 
I can count on one hand the amount of times I've said "I don't know" where PHP is concerned, this is one of those times ...

Real sorry man, I'm totally lost when it comes to complicated maths, and I've never used matlab, it's not the sort of thing I come across ... ever ...

I probably can't make sense of it because I dont know matlab, if you can explain what the math is without using shorthand from anything I might be able to help, or point you in the right direction for help, to be honest I wouldn't even know where to send you, it seems quite specialist to me ...

I am certain though, that PHP will be able to carry out these operations with little to no difficulty, I just can't make sense of your example, might aswell be written in martian ...
 
I'm sure you could, with using arrays.
Since it has the basic functions of * \ + -
I'm not sure, but I came up with this... I don't know what the answer is though, what's the answer?

PHP:
<?php 
     
$m1 = "2"; 
$m2 = "3"; 
$m3 = "2.5"; 

$k = "10"; 
$g = "9.81"; 


$A = array("1"=>"2*$k",  
            "2"=>"-$k", 
            // "3"=>"-$k  2*$k  -$k", 
            "3"=>"-$k",
			"4"=>"$k"); 
			
$A2 = array("1"=>"-$k",
			"2"=>"2*$k",
			"3"=>"-$k");
         
$b = array("1"=>"$g*$m1",  
            "2"=>"$g*$m2",  
            "3"=>"$g*$m3"); 
		
$aAll = $A2['1'] + $A2['2'] + $A2['3'];
$x2 = $b['1'] + $b['2'] + $b['3']; 
$A3 = $A['1'] + $A['2'] + $aAll + $A['4']; 
$x = $A3/$x2; 
echo $x; 

?>

I got the answer:-0.54366292898403

I have no idea if that's right.
 
Last edited:
"\" is left division, used in Matlab. Not the same thing as "/". If I remember correctly, A/B = (B'\A')'. Where the ' denotes that it is the transpose of that matrix. The answer is actually a matrix (the problem was dealing with fluid concentrations in I remember correctly) that would be in the form of:

Code:
[x1]
[x2]
[x3]

I could find the answer without left division if I could take the inverse of a matrix, as I do in Matlab with the command "inv(A)". The inverse of the matrix involves switching some of the diagonal components, changing signs, multiplying by one over the determinant of the matrix, etc. Probably to confusing to try to explain.

I didn't mean to bombard you guys with complicated questions and a slew of confusing math. I was just hoping maybe someone here would be able to give me some answers. I'm currently at my parents house and on dial-up (yes, sadly, it still exists) and searching through websites to try and find answers on my own is painfully slow. :(
 
It's nice to be asked a question I don't have the answer to actually ... once in a while anyway ...

TIP: since your looking for textual information, disable images in your browser if you get to a image diagram reload that one page with the images enabled ... I have to endure this process a LOT when using my mobile as a modem ...
 
Back
Top