Assigning a placeholder variable

I am by no means a Matlab expert so I apologize if this is a question with an easy answer, but I couldn't find any help online.
I have y=x*exp(-(x^2))+x for all x
y is given by previous functions and is some real number
however,x is not yet defined, and I can't get the formula into x= form How can I input this formula without having x defined yet? Is there a way to define x as an empty value and then impute the formula, getting the actual value for x? What am I missing here?
EDIT: I got it to work using fzero. Now I am trying to rework the code to get it to work when y exists as a range of known values instead of one known value. It looks like fzero can't be vectorized, so I tried using a for loop. I get the results, but r0 gts printed into the command line for each value of rm. I want to create a vector of all of the r0 values (and then all of the x0,y0 values). Is this possible?
Here is the code I currently have:
mu=2
nsamples=100;
for rm=linspace(-2,2,10000)
funct=@(r0,rm)mu*r0.*exp(-r0.^2)+r0-rm
options=optimset('Display','off')
[r0]=fzero(@(r0) funct(r0,rm),-2)
end

3 commentaires

Sara
Sara le 26 Juin 2014
Do you mean that you want to solve for x, given y? fzero can help you with that
José-Luis
José-Luis le 26 Juin 2014
Modifié(e) : José-Luis le 26 Juin 2014
By xexp did you mean x * exp?
Daniel
Daniel le 27 Juin 2014
Modifié(e) : Daniel le 27 Juin 2014
Ok, I got it to work using fzero. Now I am trying to rework the code to get it to work when y is a vector of known values, instead of one known value. It looks like fzero can't be vectorized. is there another way of of tackling this problem with a vector?

Connectez-vous pour commenter.

Réponses (2)

José-Luis
José-Luis le 26 Juin 2014
Modifié(e) : José-Luis le 26 Juin 2014

0 votes

your_fun = @(x) x.*exp(-(x.^2) ) + x
your_fun(3)
Please accept the answer that best solves your problem.

3 commentaires

Daniel
Daniel le 27 Juin 2014
This works if I set the x value after the fact (as in your example). I have a known y value, so I just had to use the fsolve function. See above for the problem I'm having with the for loop/not being able to vectorize the fsolve function
Image Analyst
Image Analyst le 27 Juin 2014
You forgot to attach your updated code.
updated code:
mu=2
nsamples=100;
for rm=linspace(-2,2,10000)
funct=@(r0,rm)mu*r0.*exp(-r0.^2)+r0-rm
options=optimset('Display','off')
[r0]=fzero(@(r0) funct(r0,rm),-2)
end
Also see above

Connectez-vous pour commenter.

Elias Hasle
Elias Hasle le 6 Nov 2018

0 votes

Couldn't you use the symbolic toolbox? E.g.:
syms x_symbol
y = <some expression of x_symbol>
z = <some other expression of x_symbol, could include the y expression etc.>
x_value = 1234
z_result = double(subs(z, x, x_value))

Catégories

En savoir plus sur Optimization dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by