Solving a nonlinear system for x and y as functions of r in MATLAB

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
3 replies · 7K views
JohanL
Messages
154
Reaction score
0
1.
a line in my code is something like

int(phi*x^2,0,2)

where i want phi to be a function which the user
has choosen. But how can i let the user choose a function? For numbers you can use input but how to do
it with a function?

2.
i want to solve a nonlinear system of equations in x,y.

exp(-2x)*(2*r*x-1)+1=8*pi*r^2 (1)
exp(-2x)*(2*r*y+1)-1=8*pi*r^2

Do you have to specify r before you solve the system or can you get a solution as function of r,

x=f(r) (2)
y=g(r)

?

x and y are parameters in a spherical symmetric metric and r are the radius so i guess you need a solution like (2)? But is that possible in matlab? Do you need to solve the system for many different r's and then plot the x and y as a function a r?


Thank you.
 
Physics news on Phys.org
1. I'm not a guru in Matlab but I don't think there is an equation parser. I know Matlab can work with other languages so maybe a code sequence in C or Java would solve your problem (in java you can find many examples on the internet ...like this one http://www.pyzdrowski.ws/asp/ictcm/graph.htm).

2. I would choose the second solution you give (solve the system for each value of r).
 
Thanks for your help!
1. Couldnt find the source code for the equation parser tho. Is it easy when you have the source code in java to use it in your MATLAB code? Do you know how this is done?

2. I have written a code that i think should solve the system but i get an error message because of fsolve.

syms L e ;

for i=1:3

r = i;

G = 2*pi/r^2*int(int((1/L^5)*L^2/sqrt(L^2-L-e/r^2),e,0,r^2*(L^2-1)),1,inf)

H = 2*pi/r^2*int(int((1/L^5)*sqrt(L^2-L-e/r^2),e,0,r^2*(L^2-1)),1,inf)

F = inline('[exp(-2.*x(1)).*(2.*r.*x(1)-1)+1-8*pi.*r.^2*G;exp(-2.*x(1)).*(2.*r.*x(2)+1)-1-8*pi.*r.^2*H]','x'); % define function

x0 = [0; 0]; % Make a starting guess at the solution

X = fsolve(F,x0) % Call optimizer

end

I think I've tried everything but i can't figure it out.
The code works fine if i remove fsolve but with fsolve i get the error

? Error using ==> inlineeval
Error in inline expression ==> [exp(-2.*x(1)).*(2.*r.*x(1)-1)+1-8*pi.*r.^2*G;exp(-2.*x(1)).*(2.*r.*x(2)+1)-1-8*pi.*r.^2*H]
? Undefined function or variable 'r'.

Thank you
 
I need a help from you people...by the way i am prashant from chennai...
my senior lecturer have given me a work to do as a final year project...
The work is to simulink a coded file which is a m-file in matlab. while doing so i come across various problems when implementing the "for iteration" in simulink...
I was using the for subsystem available in simulink in ports and subsystems...
In that Number of iterations can be given from an external source...


for G=0.2:0.2:1
Va =linespace(0,28-Tac/8,200)
Ia =solkar(Va,G,Tac)
plot(Va,Ia)

This is the m-file coding...This i need to do it in simulink...
how to set values for va which varies from 0 to 28-TAC/8 which has 200 divisions...
I mean how to give the incremented value of Va each time during iteration...
In short how to implement linespace in simulink...