Plotting a user defined functions variables
17 views (last 30 days)
Show older comments
Basically I want to plot the points (1,1,n1), (1,2, n1), (2,2, n1), (2,1, n1),(1,1, n2), (1,2, n2), (2,2, n2), (2,1, n2) to make a cube, where n1 and n2 are the inputs of a user defined function. The function itself isn't required to output any values.
Now my function is
function s=set(h1,h2)
s=n1.^2+n2.^4
and my code for the cube is
x=[1,2,2,1,1,1;2,2,1,1,2,2;2,2,1,1,2,2;1,2,2,1,1,1];
y=[1,1,2,2,1,1;1,2,2,1,1,1;1,2,2,1,2,2;1,1,2,2,2,2];
z=[n1,n1,n1,n1,n1,n2;n1,n1,n1,n1,n1,n2;n2,n2,n2,n2,n1,n2;n2,n2,n2,n2,n1,n2];
Now I was curious since obviously this doesn't work, must I define n1 and n2 in my function and use something else as the input or is there a way to get it to reference n1 and n2. Im fairly new to matlab so im trying to get the hang of the basics still.
Thanks a bunch
0 Comments
Answers (2)
Wayne King
on 31 Mar 2012
Hi, Welcome to MATLAB.
You should not name a function the same name as a function already in MATLAB,or on your path. To test this for set, you can enter
>>which set
set is used in MATLAB for graphics. Now in your function
function s = myset(h1,h2)
s=n1.^2+n2.^4
end
You have the function inputs as h1 and h2, but then inside of the function you use n1 and n2. If you use h1 and h2 in the function definition, then you have to use h1 and h2 inside the function.
function s = myset(h1,h2)
s=h1.^2+h2.^4
end
The user can enter any variable they want in myset(h1,h2) because those are place holders and myset.m treats the 1st variable as h1 and the second as h2.
Next please format your code using the {}Code tool and show us exactly what error you are getting including error messages, this helps people to give you the help you need.
0 Comments
Md. Sajidur Rahman
on 2 Jan 2023
clc;
close all;
clear all;
x = linspace(0,4);
y1 = x.^3 -2*x-5;
plot(x,y1);
pause(4);
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!