Plotting a user defined functions variables

조회 수: 23 (최근 30일)
Daniel
Daniel 2012년 3월 31일
답변: Md. Sajidur Rahman 2023년 1월 2일
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

답변 (2개)

Wayne King
Wayne King 2012년 3월 31일
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.

Md. Sajidur Rahman
Md. Sajidur Rahman 2023년 1월 2일
clc;
close all;
clear all;
x = linspace(0,4);
y1 = x.^3 -2*x-5;
plot(x,y1);
pause(4);

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by