0

I tried to plot the equation (x-​1)/​(y+​2)^​1.8 in octave using surf() method. But the graph is something else.

Here's my code:

p = linspace(1,50, 100);
t = linspace(1,48,100);
ans = zeros(length(p), length(t));
ans = compute_z(p, t, ans);
figure;
surf(p, t, ans');

trying to compute z = (x-​1)/​(y+​2)^​1.8 using helper function compute_z

function [ans] = compute_z(ans, p, t)
        for i = 1:length(p)
            for j = 1:length(t)
               ans(i,j) = (p(i) - 1) / (t(j)+2)^1.8;
            end
        end

I was trying to generate this graph.

m.s.
  • 16,063
  • 7
  • 53
  • 88
CodeNinja101
  • 1,091
  • 4
  • 11
  • 19

2 Answers2

4

There is no need for your compute_z-method as you can you meshgrid and vectorisation.

p = linspace(1,50, 100);
t = linspace(1,48,100);

[P, T] = meshgrid(p,t); Z = (P-1) ./ (T+2).^1.8;

figure;
surf(P, T, Z);

(Tested in Matlab, but should work in Octave as well)

harre
  • 7,081
  • 2
  • 16
  • 28
0

Your problem is you define compute_z inputs as ans, p and t in that order. However, you call the function with p, t and ans in that order. Do you see the problem?

ans isn't an input to the function, just an output, so no need to list it as an input. Furthermore, don't call your variable ans, it's the default variable name MATLAB uses when no output variable name is specified, so is likely to be overwritten.

Here's my suggestion:

p = linspace(1,50, 100);
t = linspace(1,48,100);
z = zeros(length(p), length(t));
z = compute_z(p, t);
figure;
surf(p, t, z');

with compute_z defined as follows:

function z = compute_z(p, t)
   for i = 1:length(p)
       for j = 1:length(t)
           z(i,j) = (p(i) - 1) / (t(j)+2)^1.8;
       end
   end
am304
  • 13,758
  • 2
  • 22
  • 40