Solving equation with a vector

22 vues (au cours des 30 derniers jours)
burningempires
burningempires le 5 Déc 2014
Commenté : Torsten le 7 Nov 2017
I'm not sure the term vector is correct, but I hope you'll see what I mean.
I want to solve F(x)=0.
F(x) contains the x i want to solve for and a y = 1:1:10 ([1 2 3 4 5 6 7 8 9 10], this is just an example, actual numbers are somewhat different) and some regular numbers.
The answer I'm looking for is a vector where x1 would be what it is for y2, x2 for y2, x3 for y3 and so on.
How do I go about this? I'm thinking fsolve but I haven't had any luck with that. Could someone point me in the right direction?
The best I can get is B=F(x)==0 which gives me a vector B for y1, y2, y3... But the f is still stuck in there.
If I have to I can post the actual equation, but since I want to know the way to do it, it doesn't seem like it matters. Maybe I'm writing y all wrong for this. I also get an error with matrix dimension during some of my tries.
Example:
F(x)=x-2y=0
y1=1 --> x1=2
y2=2 --> x2=4
y3=3 --> x3=6
x=[2 4 6]
Thank you in advance! :)
  1 commentaire
burningempires
burningempires le 5 Déc 2014
I thought I'd try to do it with my example.
y=1:1:3
i=length(y)
x=zeros(1,i)
x0=1
B =@(x) x-2.*y==0
x=fsolve(B,x0)
Obviously I'm not understanding what it is I'm really trying to do. This is as far as my brain allows me to go.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 5 Déc 2014
One way:
yv = 1:1:10;
for k1 = 1:length(yv)
y = yv(k1);
f = @(x) x - 2*y;
x(k1) = fzero(f, 1);
end
  8 commentaires
burningempires
burningempires le 8 Déc 2014
I think you're right in that. About a month ago I was on here asking for help with another thing, and then I think I wanted the function when there was a vector. Would it work for those or is there nowhere to get rid of the whole grid? I will thoroughly go through your previous answer here and do every step on it's own to see what it is that really happens.
You're so fast at answering and you've been extremely helpful with everything my mind can't figure out.
Star Strider
Star Strider le 8 Déc 2014
If you have a linear matrix equation, MATLAB is designed to solve them, so that will work.
To solve an equality like yours using meshgrid required a bit of figure property spelunking, but contour provides a (literal) solution:
x = linspace(-1, 1);
[X,Y] = meshgrid(x);
Z = X.^2 + Y.^2 - 0.5;
figure(1)
[C,h] = contour(Z, [0 0]);
axis equal
The function calculates the values of f(x,y)=x^2+y^2-0.5, as if solving for the x and y values that are the values of the function at 0.5. The contour function then specifically plots the contour only at 0 (because I asked it to), and the C argument gives the only contour line it creates, that being the solution (here the zeros) of the function. C here is a (2x282) matrix of x and y values (except for the first column that gives the level in its first row and the number of vertices — 281 — in the second).
So, you can use meshgrid to solve some equations, but it requires contour (or perhaps other functions that may have that ability and that I haven’t yet discovered) to do it.
Thank you. I do my best, but some (like this comment) take a bit of research — and therefore time — to provide.

Connectez-vous pour commenter.

Plus de réponses (1)

horn hosanna
horn hosanna le 6 Nov 2017
Create two row vectors: a=2:3:17 and b=3:4:15. Then, by only using
the name of the vectors (a and b), create a row vector c that is made
from elements of a followed by the elements of b.
  1 commentaire
Torsten
Torsten le 7 Nov 2017
https://www.quora.com/How-do-I-merge-two-arrays-in-MATLAB
Best wishes
Torsten.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by