So we've learnt how to plot a 2-D graph.Now here we are to plot a 3-D graph.In this case we require three variables x,y,z.
where x,y,z are mutually dependent variables or at least one of the depends on both the other variables.
1.surf(x,y,z)
plots the surface formed by x,y and z interdependent variables.
2.meshgrid()
A. This function is used to manipulate and arrange the coordinates in a matrix form.
If you want to specify the range of x only,where y and z depends on the value of x:
x=meshgrid(-2:0.1:2);
and this will give you the 5x5 matrix with the column elements of each row being -2:0.1:2.
let's draw a parabola out of this in 3-D.Since the general equation for a parabola is y=x^2 and this is not matrix multiplication but element-by-element multiplication of x co-ordinates.for more reference:
where x,y,z are mutually dependent variables or at least one of the depends on both the other variables.
1.surf(x,y,z)
plots the surface formed by x,y and z interdependent variables.
2.meshgrid()
A. This function is used to manipulate and arrange the coordinates in a matrix form.
If you want to specify the range of x only,where y and z depends on the value of x:
x=meshgrid(-2:0.1:2);
and this will give you the 5x5 matrix with the column elements of each row being -2:0.1:2.
let's draw a parabola out of this in 3-D.Since the general equation for a parabola is y=x^2 and this is not matrix multiplication but element-by-element multiplication of x co-ordinates.for more reference:
hence:
y=x.^2; %element-by-element multiplication
z=x.^2; %element-by-element multiplication
%plotting the parabola
surf(x,y,z);
%label the axes
xlabel('x');
ylabel('y');
zlabel('z');
and you get the 3-D plot.
B.If you want to specify the range of x and y and z depends on both of them then:
[x,y]=meshgrid(-2:0.1:2,-2:0.1:2);
let's try to draw this parabola:
z=x.^2+y.^2; %element-by-element multiplication
%plotting the parabola
surf(x,y,z);
%label the axes
xlabel('x');
ylabel('y');
zlabel('z');
2.Checking the plot and playing with them:
When you get the figure,go to tools->Rotate 3-D.You will find that the cursor has changed to a spiral one.Hold the left mouse button(right-handed person) on the picture and rotate your plot to see from various directions.
3.plotting a sphere:
sphere(n)
will plot a sphere of unit radius withn-by-n faces.
eg., sphere(20)
increase the number and see the effect.You can also try meshgrid to draw a sphere of desired radius in the desired location.
PLEASE LEAVE A COMMENT :)
2 comments:
Write a function named solveys that has 3 inputs:
-a vector consisting of the coefficients [ m k ] of line equation of the form : y=mx+k
-a vector consisting of the coefficients [ a b c ] of quadratic equation (parabola) of the form : y= ax^2 + bx + c
- a vector of x values
a) The function should output the vector giving x and y values of the point of intersection between line and parabola. A line and parabola can intersect in one or two points or there is no intersection. In the last case, return the empty vector.
b) Your function should also plot the line and parabola using the inputted vector of x values as a x. In addition on the same graph, plot intersection points (s) of the line and parabola. Make the line red, and parabola blue, and the intersection point (s) as magenta star. Also make sure to label your plot with
_Title : Intersection of line and parabola
x and y axis label
a legend Line and Parabola
Please Helppppppppppppppppp
Here's what I came up with.But I made a few modifications so that you can know the difference and learn from this programming.Any queries always welcome but please don't ask me the whole programming.Have a nice day...
function [x,y]=solveys(liness,parabolaas,inpx)
if length(liness)~=2 |length(parabolaas)~=3
error('Err');
end
m=liness(1);
k=liness(2);
a=parabolaas(1);
b=parabolaas(2);
c=parabolaas(3);
[x1,y1]=solve('y=m*x+k','y=a*x^2+b*x+c');
f1=inline(x1);
x2=f1(a,b,c,k,m);
f2=inline(y1);
y2=f2(a,b,c,k,m);
if imag(x2)~=0 | imag(y2)~=0
error('They do not intersect');
else
x=x2
y=y2
for i=1:length(inpx)
stline(i)=m*inpx(i)+k;
parab(i)=a*inpx(i)^2+b*inpx(i)+c;
end
plot(x,y,'p','MarkerEdgeColor','g','MarkerFaceColor','g');
hold on;
plot(inpx,stline);
hold on;
plot(inpx,parab,'red');
hold off;
title('i dont remember');
xlabel('the x axis');
ylabel('the y axis');
end
Post a Comment