Please help - surf and 3d plotting
6 次查看(过去 30 天)
显示 更早的评论
I am stuck on step 9..
I have a convergence point. Now I can't figure out how to plot in 3d or how to use the surf command properly.
Here is what I have so far:
%STEP 3
fid = fopen('c7dataFile.txt', 'r');
line = fgetl(fid);
Sphere = fscanf(fid, '%f');
for k = 1:Sphere
line = fgetl(fid);
A = fscanf(fid, '%f');
b(1:4,k) = A;
end
%STEP 4
X=[0 0 0]';
%STEP 5-8
for h=1:10
for j=1:Sphere
Y(j,1)=sum((b(1:3,j) - X).^2)-b(4,j)^2;
H(j,1:3)=[2*(b(1:3,j)-X)];
end
X=X+inv((H'*H))*H'*Y;
end
c7dataFile contains the following:
Data File for Chapter 7 Assignment
3 : Number of spheres
0 0 0 2.5 : x, y, z, r for earth
-3.0 1.0 2.5 1.75 : x, y, z, d for satellite 1
-2.5 -2.0 2.0 2.25 : x, y, z, d for satellite 2
-2.0 -1.0 3.0 1.75 : x, y, z, d for satellite 3
The problem goes:
1. Create an input file using notepad or some other text editor. Make sure the file type is ‘txt’ and not some other file type. Other file types are likely to have hidden characters that will make your script unable to read the data. Put this into a text file called ‘c7dataFile.txt’. Do not change the file name or any contents. I will be using my input file, not yours, to run your script so if you make changes to the input file, your script will not run properly when I grade your work.
Data File for Chapter 7 Assignment 3 : Number of spheres 0 0 0 2.5 : x, y, z, r for earth -3.0 1.0 2.5 1.75 : x, y, z, d for satellite 1 -2.5 -2.0 2.0 2.25 : x, y, z, d for satellite 2 -2.0 -1.0 3.0 1.75 : x, y, z, d for satellite 3
Note that the xyz values for real GPS data is different than in this assignment, but the basic idea is similar to what we are doing.
2. Start a new matlab script and put your name in the comments section at the top of the script along with your partner’s name, if you have a partner.
3. One of the first things your script will need to do is read the data from the input file. Read the data from the file into appropriate variables or arrays. I used the functions fopen, fgetl and fscanf to do this. Also, use for-end commands or some other loop syntax so that the input works even if the number of spheres changes. The number of spheres that I will test your code with is 3 or 4 (counting the earth’s surface as one of the spheres), and your program should work either way. Ideally your program should still work with five or more spheres.
4. Pick an initial estimate for the GPS receiver location (You could use the origin, though using the average location of sphere center points will converge faster).
5. Using the initial estimate for the GPS receiver location, the data and the formulas (5) and (6), create the Y vector (the residuals) and the H matrix.
6. Use the Normal Equation (equation (3)) to replace the initial estimate with better values.
7. Look at the new values for the GPS receiver location. If they seem like the might be an improvement over the first estimate, then suppress all other output and add a loop around steps 5 and 6. Set the loop to execute ten times at most.
8. Run the program again and look at the output. The xyz values of the GPS receiver location should converge quickly to a solution, getting better each time through the loop.
9. Once your solution is converging, make a 3D plot the first sphere (I used the surf command). Note that the figure window includes a button to activate the 3D rotation tool, allowing you to drag the plot with the mouse into an orientation that looks good. Label the axes and provide a title.
10. On the same plot, plot small spheres or some other symbol around each satellite location and around the point that you calculated. Draw a line from each satellite to the point you calculated (I used plot3D to do this).
11. Test your program by changing the 3 in the input file to a 4. Save the text file after making the change and then run your script. I’ve included sample plots below.
12. Use the text command to label the satellite locations and the intersection point.
13. Once the program is working, move all of the calculations related to equation (3) into a separate function file. Your main script should use that function to determine the intersection point by sending data to that function with a command similar to this one:
[xp yp zp] = findIntersection(x, y, z, r, numSatellites, estimate);
The main script will do everything except those calculations. It reads from the file and it plots the results.
14. Create two more function files, an input function and an output function. The input function reads data from the input file and returns it to the main script. The output function receives the output data from the main script and creates the plots from that data.
2 个评论
Chad Greene
2017-3-13
Whoa, it's quite unlikely anyone will read such a long post. Pair it down to a specific question and people will be able to give you specific guidance.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Reference Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!