Problem with matrix and mesh

Attached (in the images) are the problem, and what the graph is supposed to look like. Here is my code:
% Given Values
C= 15 *(10^-6);
Vm= 24;
L= 240 * 10^-3;
R=[10:1:40];
f=linspace(60,110,31);
w= 2*pi*f;
% Given functions
Top= Vm;
Bottom=sqrt((R.^2)+(w*L-(1./(w.*C))).^2);
I=[Top./Bottom]'
mesh(w,R,I)
When I run it I get this error:
Error using mesh (line 75)
Z must be a matrix, not a scalar or vector.
Error in ICA_4 (line 12)
mesh(w,R,I)

 Risposta accettata

Image Analyst
Image Analyst il 28 Gen 2017
Modificato: Image Analyst il 28 Gen 2017
You were very close. You just had an error using mesh() instead of meshgrid as instructed. I corrected that plus a few other small things:
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Given Values
C = 15E-6;
Vm = 24;
L = 240E-3;
R = [10:1:40]
f = linspace(60,110,31);
[f2, R2] = meshgrid(f, R);
w = 2*pi*f;
w2 = 2*pi*f2;
% Compute function surface.
Top = Vm;
Bottom = sqrt(R2.^2 + (w2*L-(1./(w2.*C))).^2);
I = (Top./Bottom)';
% Now plot everything.
surf(R, w, I);
ylabel('omega (angular frequency)', 'FontSize', fontSize);
xlabel('R', 'FontSize', fontSize);
zlabel('I', 'FontSize', fontSize);
ydir ='reverse';
view(gca,[-133.1 34.8]);
grid(gca,'on');

2 Commenti

Pape Traore
Pape Traore il 28 Gen 2017
Thank you so much but does mesh grid exactly and what is ydir and gcc
gca is the current axes. ydir just reverses the direction of the y axis. Actually I should have had this, to turn the omega axis around:
ax=gca;
ax.YDir ='reverse';

Accedi per commentare.

Più risposte (1)

Jan
Jan il 27 Gen 2017

0 voti

Your w, R and I are vectors with 31 elements. The first input of mesh must be a matrix containing the Z values to the grid of x and y values.
The text of the assignment mentions, that meshgrid should be used.
Notes:
  • 15 *(10^-6) is an expensive power operation, while 15e-6 is a cheap constant. The runtime does not matter when you define one constant, but it is a good programming style to consider this.
  • 10:40 looks easier and is even faster than [10:1:40], see why-not-use-square-brackets.

Categorie

Scopri di più su Particle & Nuclear Physics in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by