Solving Nonlinear System of Equations for x and y as a Function of r

Click For Summary

Discussion Overview

The discussion revolves around solving a nonlinear system of equations for variables x and y as functions of r, particularly in the context of MATLAB programming and Simulink implementation. Participants explore how to allow user-defined functions, handle parameters, and troubleshoot coding issues related to the equations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks to allow user input for a function in MATLAB, questioning how to implement this compared to numerical inputs.
  • Another participant suggests solving the nonlinear equations for discrete values of r rather than finding a general solution.
  • A participant expresses difficulty in integrating Java code with MATLAB for parsing equations and seeks clarification on the process.
  • One participant shares their MATLAB code and describes an error encountered with the fsolve function, specifically related to the undefined variable 'r' in the inline function definition.
  • A new participant introduces a separate issue regarding implementing a for loop in Simulink, specifically how to create a variable range for plotting values derived from an m-file.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to allow user-defined functions in MATLAB or how to effectively implement the for loop in Simulink. Multiple competing views and unresolved issues remain regarding the coding challenges presented.

Contextual Notes

Limitations include the lack of clarity on how to define user functions in MATLAB, the integration of external code, and the specific requirements for Simulink implementation. Participants express uncertainty regarding the correct usage of MATLAB functions and the handling of parameters within their code.

Who May Find This Useful

Readers interested in MATLAB programming, nonlinear systems of equations, Simulink modeling, and troubleshooting coding issues may find this discussion relevant.

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...
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
10
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K