Solving equation with a vector

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 Comment

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.

Sign in to comment.

 Accepted Answer

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 Comments

Amazing. Thank you for the help, did it with the real equation just fine and the results match with what I found on my calculator.
My pleasure!
The most sincere form of thanks here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
Done! Learning the social codes on a new forum can be a bit tricky :)
Thank you!
You have an intriguing username.
burningempires
burningempires on 8 Dec 2014
Edited: burningempires on 8 Dec 2014
Have a question that involves what I tried to do here. Actually managed to solve one project that used meshgrid and quiver today by myself and it just seemed so much easier. Is there any way one could make use of meshgrid in these situations? It felt easier than what you wrote here, but I also would understand if that's just not how it works. Oh, didn't even notice you had commented. Thank you, I think!
You’re solving for an unknown here, and since many Questions here are proxy for different problems, I opted to use fzero since you started with fsolve. (Here for instance, x=2*y so there is actually no need for a numerical solver.)
For plotting and some other applications, meshgrid is useful if not required:
x = linspace(-1, 1, 20);
X = meshgrid(x);
Y = 0.5*X; % Your Equation
figure(1)
surf(X, Y)
I don’t see how it helps to solve your equation here, though.
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.
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.

Sign in to comment.

More Answers (1)

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 Comment

https://www.quora.com/How-do-I-merge-two-arrays-in-MATLAB
Best wishes
Torsten.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!