I want to compute the gradient of the electrostatic potential of combination of 4 charges located at (1,1,0), (1,-1,0), (-1,1,0) and (-1,-1,0). How can I use the symbolic toolbox in MATLAB to achieve this?
My electromagnetics is rusty, but your question has a simple analytical solution.
The electric potential is:
and this is what it looks like on the plane z=0
Now the gradient is
and noting that
you can easily apply the above to all the terms in the equation of the gradient to get a closed form solution that can be easily plotted.
In MATLAB:
Here's an example that shows you how to perform the above partial differentiation in MATLAB. You can then build upon this to derive the full solution. I'll leave that upto you.
syms x y z x0 y0 z0
diff(1/sqrt((x-x0)^2+(y-y0)^2+(z-z0)^2),x)
ans =
-(x - x0)/((x - x0)^2 + (y - y0)^2 + (z - z0)^2)^(3/2)
Related
I have an equation:
b*cos(alpha) - a*sin(alpha) + b*cos(betta)-a*sin(betta) - b*cos(gamma) + a*sin(gamma) = 0
I want to create a 3D plot of this in Matlab with alpha vs betta vs gamma ( x - y - z ). I don't understand how to represent the equation so it could be plotted. How can I do this?
It is possible to assume that a = b = 1;
You need to understand what you have at hand. What does the equation show? Which form is it written? How does MATLAB plot different type of equations?
Fist, lets try to understand what type of function you have. It has 3 variables, but it equals to zero. A 3 variable equation generally defines a surface in 3D. In your case, this surface is described on its implicit form.
Now, if we look at the documentation of MATLAB, surfaces are generally plotted with surf, but surf needs 3 inputs (x,y,z) and you can not easily isolate your 3 variables.
Ah! but luckily, there is a thing called a search engine, that can gives us hints. Now that we know what kind of equation we have, we might as well use Google (or your favorite search engine) and type "implicit surface plot MATLAB", and that search will return a function called fimplicit3.
I think it seems to work:
I am using contourf to generate a contour plot for a 2 variable function.
My function is Z = f(x,y).
I generate x and y through meshgrid function in matlab and generate values for Z and then plot the contour using contour(x,y,z).
I want to be able to calculate the volume under this generated contour. Can anyone please help ?
Thanks in advance
couldn't you simply use a integral approximation like a riemann sum? Assuming uniform spacing for x and y something like this should work
delta_x = x(2) - x(1);
delta_y = y(2) - y(1);
vol = sum(Z(:)) * delta_x * delta_y;
This will not be the EXACT volume, but an approximation. Since you know your function you would get a more accurate answer by performing the integration of the function. But if you did not know the function you would use this method or any other numerical integration method.
From calculus we know that an actual integral is just a reimann sum where the width of each interval is infinitely small, so this should be a valid approximation
I have two arrays:
E= [6656400;
13322500;
19980900;
26625600;
33292900;
39942400;
46648900;
53290000]
and
J=[0.0000000021;
0.0000000047;
0.0000000128;
0.0000000201;
0.0000000659;
0.0000000748;
0.0000001143;
0.0000001397]
I want to find the appropriate curve fitting for the above data by applying this equation:
J=A0.*(298).^2.*exp(-(W-((((1.6e-19)^3)/(4*pi*2.3*8.854e-12))^0.5).*E.^0.5)./((1.38e-23).*298))
I want to select the starting value of W from 1e-19
I have tried the curve fitting tools but it is not helping me to solve it!
Then, I selected some random values of A0=1.2e9 and W=2.243e-19, it gave me a better results. But I want to find the right values by using the code (not the curve fitting Apps)
Can you help me please?
A quick (and potentially easy) solution method would be to pose the curve fit as a minimization problem.
Define a correlation function that takes the fit parameters as an argument:
% x(1) == A0; x(2) == W
Jfunc = #(x) x(1).*(298).^2.*exp(-(x(2)-((((1.6e-19)^3)/(4*pi*2.3*8.854e-12))^0.5).*E.^0.5)./((1.38e-23).*298));
Then a objective function to minimize. Since you have data J we'll minimize the sum-of-squares of the difference between the data and the correlation:
Objective = #(x) sum((Jfunc(x) - J).^2);
And then attempt to minimize the objective using fminsearch:
x0 = [1.2E9;2.243E-19];
sol = fminsearch(Objective,x0);
I used the guesses you gave. For nonlinear solutions, a good first guess is often important for convergence.
If you have the Optimization Toolbox, you can also try lsqcurvefit or lsqnonlin (fminsearch is vanilla MATLAB).
Say for example I have data which forms the parabolic curve y=x^2, and I want to read off the x value for a given y value. How do I go about doing this in MATLAB?
If it were a straight line, I could just use the equation of the line of best fit to calculate easily, however I can't do this with a curved line. If I can't find a solution, I'll solve for roots
Thanks in advance.
If all data are arrays (not analytical expressions), I usually do that finding minimal absolute error
x=some_array;
[~,ind]=min(abs(x.^2-y0))
Here y0 is a given y value
If your data are represented by a function, you can use fsolve:
function y = myfun(x)
y=x^2-y0
[x,fval] = fsolve(#myfun,x0,options)
For symbolic computations, one can use solve
syms x
solve(x^2 - y0)
Assuming your two curves are just two vectors of data, I would suggest you use Fast and Robust Curve Intersections from the File Exchange. See also these two similar questions: how to find intersection points when lines are created from an array and Finding where plots may cross with octave / matlab.
I have an equation of the form y=ao+a1logx+a2log(2/x); Is there away to fit this kind of equations?
I tried to use polyfit but finding the coefficients ao,a1 and a2 is difficult for me.
Please Help me.
What toolboxes are available to you?
The easiest way would probably be the cftool. (Type it into your command window) if you have the curve fitting toolbox. But polyfit should do as well.
The main problem I see: Your coefficients are not independent of one another. Because log(2/x) is equal to log(2) - log(x) your equation becomes:
y = ao + a1*log(x) + a2*log(2) - a2*log(x);
which is equivalent to:
y = bo + b1*log(x);
Try that one.